Types¶
stone is statically typed, but you never write a type. The checker infers one type for every variable, parameter, and return value before the program runs, and reports every place they conflict.
The types¶
type |
values |
|---|---|
|
64-bit signed integers. |
|
IEEE 754 doubles, including |
|
|
|
immutable byte strings |
|
|
|
growable lists of |
functions |
each function has one signature, such as |
Inference¶
Each name gets one type for its whole scope, from the first thing assigned to it and every use after. Assigning a different type later is an error:
x = 1
x = "one" # error: cannot assign str to 'x', which is int
An empty list [] starts with an unknown element type, which is filled in by what is appended
to it, assigned into it, or what it is passed to.
Functions are monomorphic: each parameter has a single type inferred from the function’s body
and its calls. A function called as f(1) cannot also be called as f("a").
Rules the checker enforces¶
These keep the two backends identical and catch common mistakes:
No implicit conversion. Arithmetic and comparisons never mix
intandfloat; useint()orfloat(). The one exception is**, whose exponent is always anint. Strings never mix with numbers either:int("3")andfloat("2.5")read numbers from strings, andstr(3)writes them.Conditions are
intorbool.if,elif,while,and,or, andnotreject floats, strings, lists, andnone.Lists are not comparable.
==and!=cannot compare lists.rangeis only aforiterable. It cannot be stored or passed around.Functions are top level, and only called. A function cannot be assigned, stored, or passed as a value, and a builtin’s name cannot be assigned to.
Calls match. Each call passes as many arguments as the function has parameters.
Returns are consistent. A function that returns a value on one path must return a value of the same type on every path.
Variables are assigned before they are read. At the top level and inside each function, every path to a read must assign the name first. A function reading a global is checked when it runs instead, since it may be called before or after the assignment.