Why does Kotlin lang allow only one main function in a project?

Doesn’t this remove the peculiarity of having several main entry points in java that can be called when necessary.

+5
source share
2 answers

UPDATE : recent versions of Kotlin allow you to perform several main functions even in one package (if they are in different files).

Your project may have several main functions, but only one main function for the package

The reason you cannot make several basic functions in a package is because all functions in a package are stored in the Package class, so you cannot have several functions in a class with the same signatures.

So, if you need some basic functions, you need to define em in different packages

+13
source

In addition to Sergey Mashkov’s comment: you can put main inside the object and mark it with @JvmStatic :

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

Source: https://habr.com/ru/post/1215983/


All Articles