Types
Primitives (the object mask)
The built-in value types, their default values, the methods they carry, and the void unit rules.
since 0.1.0-alpha.1linuxwindowswasm
Description
These types are primitives: int, string, bool, float, char, byte, int8, int16, int32, uint, float8, float16 and float32. A primitive is a value type. Assigning one copies it. A literal has a primitive type, and you call methods on a primitive with the same dot syntax as on a class: 5.toString(), "abc".length(), 2.5.floor().
The primitive type names are ordinary identifiers declared by the standard library, not keywords. The methods of each primitive are listed in the library reference, under std.int, std.float, std.bool, std.char, std.string and the sized types.
A variable declared without an initializer holds its type's default value:
| type | default |
|---|---|
int, byte, int8, int16, int32, uint |
0 |
float, float8, float16, float32 |
0.0 |
bool |
false |
string |
"" |
char |
the scalar 0 |
Defaults and method calls
int i;
float f;
bool b;
string s;
byte y;
console.writeln(i);
console.writeln(f);
console.writeln(b);
console.writeln("[" + s + "]");
console.writeln(y);
console.writeln(5.toString().length());
console.writeln((-3).abs());
console.writeln(2.5.floor());
0
0.000000
false
[]
0
1
3
2.000000
Rules
intis a signed 64-bit integer.floatis an IEEE 754 binary64 number.boolistrueorfalse.stringis a sequence of bytes, usually UTF-8 text.charis one Unicode scalar. The sized integer and float types are described inlang.sized-integersandlang.sized-floats.- Primitives are values: after
int b = a;the two variables are independent. - Primitive type names are not keywords. They resolve like any other type name.
voidis the absence of a value. It is written as a return type. It may also be a generic argument (Promise<void>,Box<void>) or a parameter type.- A
void-typed parameter accepts exactly one argument,None, the unit value:note(None). The same holds for a parameter whose type isvoidafter a generic substitution. The value received isNone. Noneis not assignable to avoidlocal variable or return type, nor to any other type that is not optional or a union containingNone.- A
void?parameter acceptsNone. A wider union such asvoid | intdoes not:h(None)is rejected. - With overloads
f(void)andf(int?),f(None)is a tie, and the first declared overload wins.
Examples
A void parameter, and void as a generic argument:
The unit value
class Box<T> {
T item;
}
void note(void v) {
console.writeln("got the unit value");
}
note(None);
Box<void> b = Box(None);
console.writeln(b.item == None);
got the unit value
true
Notes
Not implemented: a default value for a void parameter. void g(void v = None) is rejected with "default value for parameter 'v' is not assignable to 'void'"; pass None explicitly instead.
See also
- int — The signed 64-bit integer type, the default type of whole numbers.
- float — A 64-bit IEEE 754 floating-point number (binary64).
- bool — The boolean type: a value is either
trueorfalse. - char — A single Unicode character, stored as one code point.
- string — An immutable sequence of text, stored as UTF-8.
- Sized integers: byte, int8, int16, int32, uint — The fixed-width integer types, how arithmetic wraps, how mixed widths combine, and when conversions throw.
- Sized floats: float8, float16, float32 — The narrow floating-point formats, how every operation re-rounds, and where they overflow or throw.