Velo by Example: Native Classes

Host (JVM) classes are bound by registration — no declarations in Velo source. The type is synthesized from the class itself and checked at compile time.

Register plain Kotlin classes on VeloRuntime — that is the whole binding.

// Kotlin
val runtime = VeloRuntime()
    .register(MyClass::class)
    .register("VeloName", JvmClass::class)

runtime.runFile("script.vel")

Use the class directly in Velo — Terminal, Time, FileSystem, Http and Socket are registered by default.

Terminal term = new Terminal();
term.println("Hello!");

Time time = new Time();
term.println(time.unix().str);

Type mapping between Velo and JVM is automatic for primitives, collections and callbacks.

# Velo            → JVM
# int             → Int
# float           → Float
# str             → String
# bool            → Boolean
# byte            → Byte
# array[T]        → Array<T>
# dict[K:V]       → Map<K, V>
# func[(T) void]  → VeloFunction / (T) -> Unit

Callbacks work both ways: a func[(args) void] handed to native code always runs back on its owning Velo thread.

Notifications n = new Notifications();
n.subscribe(func(str text) void {
    term.println("got: ".con(text));
    void
});