Threadon / Std lib

Std lib

the small standard library

What's in it

The standard library is a single Threadon file, stdlib/std.th, written in the language itself. It has six functions, all taking Int32:

FunctionReturnsExample
abs(x)Int32, the absolute value of xabs(-5)5
max(a, b)Int32, the larger of the twomax(3, 7)7
min(a, b)Int32, the smaller of the twomin(3, 7)3
clamp(x, lo, hi)Int32, x forced between lo and hiclamp(10, 0, 5)5
is_even(n)Boolis_even(4)1
is_odd(n)Boolis_odd(4)0

It's loaded automatically

The std lib is imported automatically into the main file of a program. You never write import std yourself; the functions are just available by their plain name.

def main() -> Int32
    n: Int32 = 127
    print(clamp(n, 0, 100))
    print(min(n, 50))
    return 0
100 50

Because it's a plain Threadon file, it follows every rule you've learned: functions before use, return types on all paths, NoneType with a bare return, and so on.

Inside modules

One catch: the automatic import only happens for the main file. If you're writing a module and want a std function, import it yourself. Both of these work inside a module:

from std import abs, clamp
import std   # then call std.abs(-5)

The built-ins print and input are not part of std.th; they're always available, in modules and main files alike.

You can shadow it

The std lib is found on the module search path like anything else. If you put a file named std.th somewhere earlier on the search path, it wins over the real one. The automatic import looks up std through the normal search, so a local std.th is picked up and its functions are what you get. Same for a folder: a std module on the path shadows the library. See Imports for how the search path works.