Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Kotlin lang allow only single main function in project?

Tags:

kotlin

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

like image 634
Dragonborn Avatar asked Mar 24 '15 08:03

Dragonborn


People also ask

Is main function mandatory in Kotlin?

For now, just remember that main function is a mandatory function which is the entry point of every Kotlin program. The signature of main function is: fun main(args : Array<String>) { ... .. ... } println("Hello, World!")

Which is not true of functions in Kotlin?

Kotlin does not infer return types for functions with block bodies because such functions may have complex control flow in the body, and the return type will be non-obvious to the reader (and sometimes even for the compiler).

What is the difference between open and public in Kotlin?

'Visible' refers to 'accessible'. And public makes it accessible from anywhere. open allows inheritance of a class.

How do you pass a function as a parameter in Kotlin?

In Kotlin, a function which can accept a function as parameter or can return a function is called Higher-Order function. Instead of Integer, String or Array as a parameter to function, we will pass anonymous function or lambdas. Frequently, lambdas are passed as parameter in Kotlin functions for the convenience.


1 Answers

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!")
    }
}
like image 131
Andrey Breslav Avatar answered Sep 20 '22 16:09

Andrey Breslav