Types

Cresset checks types before your program runs, but you don't have to write them. It works out what it can from your code, and only complains when it is certain something is wrong.

It infers types

fn main() needs print:
    n = 5
    words = "a b c".split()
    print(n + 1, words.len())

Cresset knows n is a number and words is a list of text, because of what was assigned. Mistakes get caught without any annotation:

fn main() needs print:
    n = 5
    print("n=" + n)
error: can't add text and a number
  --> example.crs:3
help: to put a number inside text, write it in braces: "total: {n}"

Optional annotations

Add types where you want a promise held. Put a type after a parameter name, and -> before the colon for the return type:

fn describe(name: text, score: num) -> text:
    return "{name} scored {score}"

fn main() needs print:
    print(describe("Ada", 9))

Calling it with the wrong kind of value is caught before anything runs:

fn describe(name: text, score: num) -> text:
    return "{name} scored {score}"

fn main() needs print:
    print(describe("Bob", "five"))
error: 'describe' expects 'score' to be a number, but this is text

The types are num, text, bool, none, any, rng (a seeded random generator), list[T], map[T], result[T], and the names of your own records and enums. For example, list[num] is a list of numbers. Plain list, map and result mean "of anything".

What it catches

It is gradual

Anything Cresset can't work out is treated as "could be anything", and anything never causes an error. That is why correct, dynamic code is never rejected:

fn pick(flag, a, b):
    if flag: return a
    return b

fn main() needs print:
    x = 5
    x = "now it is text"
    print(pick(true, 1, "a"), x)

Limits

Next

Make your own types: Records, enums and match.