Users › 09 Std and packaging

Std and packaging

Print anything, use the String API, and ship compiled modules.

1. The standard library is small on purpose

Core Lopolith has no I/O. Everything you have printed so far came from Std, a native singleton in lib/Std.so:

#import Std
Std.print("x = ", x, ", y = ", y, "\n")

print is variadic: any number of arguments, any types. Each argument is formatted by its static type:

Argument typeOutput
booltrue / false
integer typesdecimal
float / doubleshortest round-trip form; nan, inf, -inf
Stringcontents; null prints null
other referencesthe class name
arrays[a, b, c], nested arrays included

Floating-point printing is the shortest representation that round-trips, and integral values never use scientific notation: 100.0 prints 100, while 1.0e30 prints 1e+30.

2. String methods

MethodBehavior
int length()byte length
int indexOf(String sub)first index or -1
String substring(int start, int end)clamped to the string bounds
String toString()the string itself
bool ___equals(String)content equality, used by ==

Remember: arrays use the field a.length, strings use the method s.length().

3. Compiled modules

loth compile writes portable bytecode for the file and its whole dependency closure:

$ loth compile com/example/todo/Main.loth -o build
wrote build/com/example/todo/Main.lothc (812 bytes)
wrote build/com/example/todo/Task.lothc (491 bytes)
... (Std, Runnable, Thread, ...)

$ loth run -e com.example.todo.Main -cp build build/com/example/todo/Main.lothc
Hello from the task list

The task-list example uses exactly this flow.

4. Try it

%simple com.example.std.Demo implements Main
    #import Std
    int run(int argc, String[] argv) Main@run {
        Std.print("int=", 42, " long=", 42L, " d=", 100.0, " f=", 0.1f,
                  " b=", true, " null=", null, "\n")
        int[] a = {1, 2, 3}
        Std.print("arr=", a, "\n")
        String s = "hello world"
        Std.print("len=", s.length(), " idx=", s.indexOf("world"),
                  " sub=", s.substring(0, 5), "\n")
        return 0
    }
int=42 long=42 d=100 f=0.1 b=true null=null
arr=[1, 2, 3]
len=11 idx=6 sub=hello