Lexical
Literals at a glance
Every literal form of the language and the type each one has.
since 0.1.0-alpha.1linuxwindowswasm
Description
A literal writes a value directly in the source. Each form has a default type:
| form | examples | type |
|---|---|---|
| integer | 42, 0, 0xFF, 0b1010 |
int |
| float | 3.14 |
float |
| string | "hello", 'hi' (both quote styles) |
string |
| boolean | true, false |
bool |
| array | [1, 2, 3], [], [1..5], [1..3, 7] |
Array<T> |
| range | 1..10 (inclusive) |
Range |
The detailed rules are in lang.numeric-literals, lang.string-literals, lang.char-literals and lang.array-and-range-literals. A literal can take a different type from its default when the context asks for one: an integer or float literal becomes a sized numeric type when it is stored in one (see lang.sized-integers and lang.sized-floats), and a one-character single-quoted literal becomes a char.
One of each
console.writeln(42);
console.writeln(3.14);
console.writeln("hello");
console.writeln('hi');
console.writeln(true);
console.writeln([1, 2, 3]);
Range r = 1..10;
console.writeln(r.end);
42
3.140000
hello
hi
true
[1, 2, 3]
10
Rules
trueandfalseare the onlyboolvalues. They are keywords.- An integer literal has type
intunless the context gives it another numeric type. - A literal containing a decimal point, such as
3.14, has typefloat. There is no exponent notation:1e3is a syntax error, so write1000.0. - A string literal has type
stringwhichever quote style is used. Only a single-quoted literal can become achar. - An array literal has type
Array<T>, and a range literala..bhas typeRange.
Examples
A float literal is never an int, and mixing them gives a float:
Default literal types
console.writeln(7 / 2);
console.writeln(7 / 2.0);
console.writeln(true == !false);
3
3.500000
true
Notes
Printing a float always shows six decimal places.