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, float32 and decimal. 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().
Ptr, the address of foreign (C) memory or code, is a primitive too; it has no literal and is described in lang.ptr.
The primitive type names are identifiers declared by the standard library, not keywords. Such a name, or void, may name a variable, a parameter, a function or a member, but no type declaration in any scope, including a type parameter (string show<int>(int a)): a class, a struct, an interface, an enum, a type parameter, a namespace or a use ... as alias named like a primitive above is a compile error, 'int' is part of the core language (a primitive type, in every scope); rename this declaration: the name may name a variable, a parameter, a function or a member, but no type. The methods of each primitive are listed in the library reference, under std.int, std.float, std.bool, std.char, std.string, std.decimal 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 |
decimal |
0 (scale 0) |
bool |
false |
string |
"" |
char |
the scalar 0 |
T() with no arguments is T's default construction, so for a primitive it yields the value in this table, the same value a bare declaration gets: int() is 0, string() is "", bool() is false, float() is 0.0, decimal() is 0. A type with no default (lang.fields) cannot be constructed this way: I() of an interface is a compile error. T(x) with an argument is not a conversion; use x.toInt(), x.toString() and the other conversion methods.
Defaults and method calls
use io::console;
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.0e0
false
[]
0
1
3
2.0e0
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, anddecimal, the exact base-10 number, inlang.decimal.- Primitives are values: after
int b = a;the two variables are independent. - A method of
string,bool,int,float,decimalor a sized numeric type never changes its receiver; it returns a new value. Discarding that value is a warning (exit status 0):s.trim();,x.pow(2);andd.round();each reportresult of '...' is discarded; a value type's non-mutating member returns a new value and leaves 's' unchanged. Keep the result (string t = s.trim();) or drop the call. The same rule covers the pure methods ofArray,MapandSet(lang.collection-value-semantics). - 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
use io::console;
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.
- decimal — The exact base-10 number for money and other decimal quantities — 34 significant digits, a kept scale, value equality and half-even rounding.