Referenz
Values and types
Bindings, the built-in types, and the type forms that are about values rather than about behaviour.
Diese Seite gibt es noch nicht auf Deutsch, deshalb steht sie hier auf Englisch. Zur englischen Seite
Bindings, every built-in type, and the type forms that are about values rather than behaviour: aliases, literal types, distinct types and arrays.
What belongs here#
What does not belong here: declaring a type of your own, which is in types/. Every page in this folder is a reference page:
an example first, then the syntax, then numbered rules, then what the construct is not.
Pages#
- Built-in types - Every type a file has without an import - the sized numbers, Bool, Char, String, tuples, lists, maps, ranges, Option, function types, Void and Never.
- Bindings - A const binding never changes and nothing below it changes; a var binding can be changed in place. That one rule replaces every mutable-and-immutable type pair.
- Integers - Eight sized integer types with fixed ranges on every platform, an unannotated literal is always Int64, and overflow is a compile error when it is written and a panic when it happens at runtime.
- Floating-point numbers - On a Float every operator is IEEE-754 and
compareis a total order that disagrees with them onnanand-0.0, and neither Float type is Hash. - Decimal - Decimal is exact base-ten arithmetic such as money; a number literal adapts to it and keeps every digit and its scale, and both back ends compute it.
- Strings - A String has no length() and no text[i], because "length" and "the i-th character" each have three different answers and two of them are slow.
- Tuples - A tuple is positional and accessed by .0, .1; a label makes a position easier to read but is not part of the type, so a labelled and an unlabelled tuple of the same shape are the same type.
- Ranges - The ends a range has are its type - Range, RangeFrom or RangeTo - so nothing is optional and nothing panics; what accepts every form takes the Bounds trait.
- Option - Absence is a value, Some(value) or None, and there is no null: a value wraps itself into Some where an Option is expected, and an Option is only ever unwrapped on purpose.
- Void and Never - Void has exactly one value, the keyword literal void, the way true and false are the values of Bool; Never has no value at all and converts to every type, which is why panic fits into any expression.
- Literal types -
"tcp" | "udp"is a type made only of specific values of one base type; only literals combine with|, because there are no unions of types. - Checked literals - A string literal where a Path, a Uri, a UriTemplate, a Regex or a resource type is expected is read by the compiler where it is written, and one that is not valid is a compile error at that line; a template and a pattern are read verbatim.
- Type aliases -
type Name = Othernames an existing type rather than declaring a new one, and the two names are freely interchangeable - there is no separatealiaskeyword. - Distinct types - A distinct type is an ordinary single-field type, and
byforwards specific traits to that field so the wrapper costs no boilerplate - there is no separate opaque-alias feature. - Arrays and const parameters - Array<Item, const Size> carries its length in the type, a const parameter is a value rather than a type, and there is no arithmetic over one.