Skip to main content

Types

Primitive, collection and user-defined types in Sarlin.

types.sar
func main() {
    local var lives uint8 = 3
    local var temperature float32 = 21.5
    local var active bool = true
    local var name string = "Sarlin"

    print(name + ": " + lives)
    print(active and temperature > 20.0)
}

Signed integers

TypeDescription
int8−128 to 127
int16−32,768 to 32,767
int32−2,147,483,648 to 2,147,483,647
int64−9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Unsigned integers

TypeDescription
uint80 to 255
uint160 to 65,535
uint320 to 4,294,967,295
uint640 to 18,446,744,073,709,551,615

Floating point

TypeDescription
float3232-bit IEEE 754 floating-point value
float6464-bit IEEE 754 floating-point value

Other primitives

TypeDescription
booltrue or false
stringImmutable, reference-counted bytes; source literals are UTF-8

Standard types

TypeDescription
TimeA reference-counted countdown or stopwatch backed by the device clock

Numeric literals and conversion

A numeric literal takes its type from its context: the declared type it initializes, the parameter type it is passed to, or the type of the other operand in an expression. Where nothing supplies a type the compiler rejects the literal rather than guessing, socount > 1 is valid while 2 > 1 is not. A leading minus is supported on numeric literals.

Sarlin automatically converts between numeric types for initialization, assignment, arguments, returns and mixed numeric expressions. A floating-point value converted to an integer is truncated toward zero. The compiler rejects a numeric literal that cannot fit in its required integer type.

Addition, subtraction and multiplication on a signed integer are checked too. A result that does not fit the type stops the program rather than silently continuing with a wrapped value, because a signed overflow is almost always a mistake.

Collection types

  • int32[] is a dynamic array.
  • int32[3] is a fixed array containing three values.
  • int32[][] is a nested dynamic array.
  • dictionary[string, int32] is a dynamic dictionary.
  • dictionary[string, int32, 4] is a fixed-size dictionary.

Classes declared in the project are also types. Defines are composed into classes and cannot be constructed directly.