Typesafe | Program

Program code is completely type safe, with a simple type system supporting native types, data records, tagged unions, and generics. The collection module then provides us with types like Optional and Array .

A small set of built-in native types are provided.

Whole numbers use Maths.Integer and fractional numbers use Maths.Real .

Textual values use Text .

A record TYPE defines a group of FIELD s, giving each FIELD a name within that record.

Variables can be constructed using a data record — name the type and supply its fields in order.

Fields of a data record variable can be accessed via their names.

A union TYPE is a closed set of variants, so a value is exactly one of them at a time.

Variables can be constructed by naming a variant and supplying its value.

Reading a variant produces an Optional — it is present only when the value is that variant — so the result must be checked before use.

Program supports generics in both type definitions and functions. A name in parentheses after the type or function name is a generic parameter — a placeholder standing in for a type the caller chooses. A generic record is written TYPE TypeName(GenericName) and a generic function FUNCTION FnName(GenericName) , and the placeholder is then usable anywhere a type is in that definition.

Here WithContent takes an Entry(Value) record and one Value , and returns a new Entry with the same label but the given content. Whatever type Value is bound to, the record's field and the argument are guaranteed to be the same type — the substitution is checked, not assumed.

Any number of generic types can be provided, separating them by space:

There is no bare null that slips through unchecked. A value that may be missing is an Optional — itself a union of present and absent — so the absence is in the type and the compiler makes you account for it. Because it is a real type, it even nests: an optional of an optional is a distinct, meaningful thing.

An Collection.Optional(Circle) is built by naming the present variant with a value, or the absent variant with none.

A function takes an optional circle and displays it — the Collection.Optional.If body runs only when a circle is present, so the absent case is handled by drawing nothing at all.

The same types map onto each target's own type system: a record becomes a struct or class , a union becomes a sum type — a Swift enum , a Rust enum , a Dart sealed class — and a built-in maps to the native primitive. So the generated code is type-safe in its own right, checked by that language's compiler too, not only at the Program layer.

Recommended articles