If we know how to call Kotlin from java, and how to call Java from Ceylon, then we can call Kotlin from Ceylon.
Suppose we have a kotlin file:
@file:JvmName("FromCeylon") // so we can freely change source file name.
package fromceylon
tailrec fun findFixPoint(x: Double): Double {
if (x == Math.cos(x)) {
return x
} else {
return findFixPoint(Math.cos(x))
}
}
Compile it to jar, including kotlin runtime:
kotlinc -include-runtime -d kotlin.jar kotlin.kt
For example, we can put it under:
modules/fromceylon/0.0.0/
|-- fromceylon-0.0.0.jar
`-- module.properties
module.properties
provides meta information of jar file.
It is simple to write:
fromceylon=0.0.0
Create a simple module with ceylon new
.
ceylon new simple --module-name com.example.hello --module-version 0.0.0
module.ceylon
native("jvm") module com.example.hello "0.0.0" {
import fromceylon "0.0.0";
}
run.ceylon
import fromceylon { FromCeylon { findFixPoint } }
shared void run() {
print("Calling Kotlin:");
Float fixPoint = findFixPoint(1.0);
print(fixPoint);
}
; ceylon compile com.example.hello
Note: Created module com.example.hello/0.0.0
; ceylon run com.example.hello
Calling Kotlin:
0.7390851332151607