javakotlinnativekotlin-native

Kotlin Native equivalent of System.exit(-1)


In the following Kotlin/JVM program System.exit(-1) stops the execution of the program with an error exit code:

fun main(args: Array<String>) {
    if (args.size < 2) {
        println("too few args!")
        System.exit(-1)
    }
    println("Hello, ${args[1]} from ${args[0]}")
}

Kotlin/Native does not have access to any Java classes, including System. So what would the equivalent function be for a Kotlin/Native program to stop execution of the program with an error code?


Solution

  • Use exitProcess:

    import kotlin.system.exitProcess
    ...
    exitProcess(exitCode)
    

    Declaration and documentation in the Kotlin source code.