Skip to content

Instantly share code, notes, and snippets.

@weakish
Last active September 17, 2016 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weakish/b4c3c2d26f2df7b287d7f1b0d9f444ea to your computer and use it in GitHub Desktop.
Save weakish/b4c3c2d26f2df7b287d7f1b0d9f444ea to your computer and use it in GitHub Desktop.
call #kotlin from #ceylon

Call Kotlin from Ceylon

tl;tr

If we know how to call Kotlin from java, and how to call Java from Ceylon, then we can call Kotlin from Ceylon.

Walk through

Compile kotlin code to jar file (runtime included)

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

Put it in ceylon module repository

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

Use it from other ceylon module

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);
}

Run it

; ceylon compile com.example.hello
Note: Created module com.example.hello/0.0.0
; ceylon run com.example.hello
Calling Kotlin:
0.7390851332151607
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment