A type is a pattern that:

Explicit declaration of types are valuable for optimizing a program's performance and space efficiency, as well as offer compile-time safety protections.

This page briefly introduces the various kinds of types and describes features that they share in common.

Kinds of Types

Most types are value types; they apply to any value used and manipulated by a program:

Reference management types mediate the use of references to ensure memory and race safety:

Execution types apply to various units of execution. Instead of defining their data content, they declare how they are to be used:

These type kinds are used like type factories, enabling definition of specific types, as detailed by the respective chapters that cover each kind of type.

Strict Typing, Coercion and Conversion

Typing is strict. In most cases, attempting to place a value of one type into a container declared as another type will generate a compile-time error. However, there are times when this is allowed:

Another way to satisfy strict typing is to use methods that explicitly convert a value of one type into a different value of the required type.

Methods

Most types allow the definition of methods that apply to values of that type. Methods are simply functions whose definition is part of a type's declaration. The method's first parameter is named self. The type of self is either a value or reference of the type it belongs to.

struct Point
  x f32
  y f32
  // a method that works on Point values...
  fn len(self) f32
    ((.x*.x)+(.y*.y)).sqrt()

fn test()
  imm pt = Point{3., 4.}
  imm c = pt.len() // use Point's 'len' method

Constructor Methods

For some types, it is useful to have methods able to create a new value of a specific type. A type is allowed to define multiple constructor methods. By convention, the most commonly used constructor is called init.

To represent that self needs to be created, it should be marked with new and should declared its type as a reference. The constructor should return self (otherwise, what's the point?).

struct Point
  x f32
  y f32
  fn init(new self &, x f32, y f32)
    self = Point{x,y}
	self

'drop' Method

Although it is not often needed, some types need the ability to perform some clean-up actions before a value is deleted (for example, releasing resources they have acquired). This clean-up activity should be performed within a method called drop.

_