Skip to content

Latest commit

 

History

History
118 lines (93 loc) · 3.19 KB

File metadata and controls

118 lines (93 loc) · 3.19 KB

Types

  1. type-value
  2. qualifier type-value
  3. value-category type-value
  4. qualifier value-category type-value

Type Values

Primitives

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.

Primitive: bool

Stores either true or false and has a (minimum) size of 1 byte.

Primitive: char

Stores an UTF-8 character and has a (minimum) size of 1 byte.

Primitive: int

Stores a signed integer ranging from −2,147,483,648 to 2,147,483,647 and has a (minimum) size of 4 bytes.

Primitive: uint

Stores an unsigned integer ranging from 0 to 4,294,967,295 and has a (minimum) size of 4 bytes.

Primitive: float

Stores a floating-point value in IEE754 binary32 format and has a (minimum) size of 4 bytes.

Classes

A class-name can also be a type-value (e.g. Dog, Vec2, FileStream etc.).

Qualifiers

A qualifier is one of the following:

Qualifier: mut

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.

Qualifier: const

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

Value Categories

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
}

lvalue: &

rvalue: &&