Why does Kotlin lang allow only single main functi

2019-04-09 04:20发布

问题:

Doesn't this take away the feature of having multiple main entry points in java which can be called as and when required.

回答1:

UPDATE: recent versions of Kotlin allow multiple main functions even in the same package (if they are in different files).

You can have multiple main functions in your project but only one main function per package

The reason why you can't make multiple main functions in package is that all functions in package are stored in Package class so you can't have multiple functions in a class with same signatures.

So if you want multiple main functions you have to define them in different packages



回答2:

In addition to Sergey Mashkov's comment: you can put a main inside an object and mark it @JvmStatic:

object Main {
    @JvmStatic 
    fun main(args: Array<String>) {
        println("Hello, world!")
    }
}


标签: kotlin