How to create batch functions?

I read the Kotlin Reference Guide , and one part said:

In Kotlin, unlike Java or C #, classes do not have static methods. In most cases, it is recommended that you simply use package level functions.

How to create a package level function?

+7
function kotlin
source share
1 answer

From the reference:

All contents (for example, classes and functions) of the source file are contained in the declared package.

So, just creating the source file as follows:

package my.pkg fun f0()=0 fun f1()=1 

We can declare functions with the names f0 and f1 directly visible in the package my.pkg . These functions can then be imported and used similarly to classes:

 import my.pkg.f0 import my.pkg.f1 

Alternatively, using the * syntax:

 import my.pkg.* 
+12
source share

All Articles