- type-value
- qualifier type-value
- value-category type-value
- qualifier value-category type-value
A primitive is a very basic type value and is the starting block of the abc type-system. Primitives include truth values, integers and decimals as well as characters characters.
Stores either true or false and has a (minimum) size of 1 byte.
Stores an UTF-8 character and has a (minimum) size of 1 byte.
Stores a signed integer ranging from −2,147,483,648 to 2,147,483,647 and has a (minimum) size of 4 bytes.
Stores an unsigned integer ranging from 0 to 4,294,967,295 and has a (minimum) size of 4 bytes.
Stores a floating-point value in IEE754 binary32 format and has a (minimum) size of 4 bytes.
A class-name can also be a type-value (e.g. Dog, Vec2, FileStream etc.).
A qualifier is one of the following:
The mut qualifier ensures that the given type is mutable,
i.e. that values of the type are allowed to change:
let myint :mut int= 0;
myint = 42; // value changes
We can also change member values:
let position :mut Vec2= Vec2(0, 0);
position.x = 0; // member value changes
The same rule applies for member functions:
class Vec2 {
// this member function is not marked as const!
func reset() {
_x = 0;
_y = 0;
}
x: float;
y: float;
}
let position :const Vec2= Vec2(0, 0);
// so we can't call it from a const variable!
/* position.reset(); */
A type is mutable by default!
In most cases, the mut keyword is not necessary.
However in some cases, it is useful:
- to put emphasis on the fact that a variable is mutable
- to set the qualifier of a generic to mutable
To understand why the mut keyword is necessary for generics, read here.
The const qualifier ensures that the given type is constant,
i.e. that values of the type are not allowed to change:
let myint :const int= 0;
// this won't compile since the variable is marked as const!
// myint = 42;
It is also forbidden to change member values:
let position :const Vec2= Vec2(0, 0);
// this won't compile since the variable is marked as const!
// position.x = 0;
TODO: member function overload with qualifier
Similar to C++, value-categories in abc are used for move-semantics.
If no value-category is specified, values are passed in-place and if a value-category is specified, values are passed as references:
func pass_in_place(x: int) {
x = 0; // no effect outside of the function
}
func pass_as_ref(x: &int) {
x = 0; // sets value of myint
}
func pass_as_ref(x: &&int) {
x = 0; // sets value of myint
}
func main() {
let myint := 42;
pass_in_place(myint); // the value 42 is copied to x
pass_as_ref(myint); // the address of myint is copied to x
pass_as_ref(move(myint)); // the address of myint is copied to x
}