System.err.println()

Why doesn’t Kotlin have something like System.err.Println () Java? I would like to have the option to use in my code, but more simply than Java.

1 Like

If you’re targeting the JVM that is actually working in Kotlin as you wrote it

I know so… But Kotlin has its standard for System.out.println() which would just be println(), what I mentioned was just System.err.println(), it could be something like printErrLn(), As if it were something from the Kotlin standard library and not something interoperable with Java.

1 Like

I don’t know if this is the reason, but maybe authors decided that separate stout and stderr streams are too specific to selected targets only. Almost every runtime has some kind of an output, but not necessarily two of them. But to be honest, I’m not entirely convinced with such explanation myself :slight_smile:

2 Likes

As you say, not overwhelmingly likely — but very possible!

Other possibilities include: not being able to think of a clear, concise name for such a function; wanting to discourage output to stderr as it’s overused; or maybe the language(s) from which they copied println() don’t have an equivalent for stderr (indicating a lack of desire for it)?

But of course, this is all speculation. I’d be interested to hear an official answer.

1 Like

if this is just about the name you can easily define your function to wrap
the java code

fun printErrln(msg: String) {
        System.err.println(msg)
}
1 Like

I know I can, but the idea is that there could be a simple line, just like for println() in the standard library.

I agree, and I think this extends beyond the JVM. What if I want to print to stderr in Kotlin/Native? That should be possible without dropping down to C.

One good naming scheme may be eprint/ln, as it’s used in some languages like Rust, or maybe errprint/ln which is even more explicit.

1 Like

Maybe add optional level parameter(enum) to print and println

something like: println(“An error occured”, OutputLevel.Error)

here’s an example implementation of it(jvm):

fun println(
    message: Any,
    level: OutputLevel = OutputLevel.Standard
) {
    when (level) {
        OutputLevel.Standard -> kotlin.io.println(message)
        OutputLevel.Error -> System.err.println(message)
    }
}

enum class OutputLevel {
    Standard, Error
}

Bumping after more than a year. There needs to be a multi-platform way of writing to stderr. Standard err is standard. And necessary for writing a tool for the middle of a pipeline. Quick search on kotlinlang.org shows that Throwable.printStackTrace() prints to “standard error output.” How does it do that? Stdlib should all be KMP compatible, right?

1 Like