Classes
A class holds fields and methods, may derive from one base class, and carries a vptr. Everything here is also covered in the language reference; this page collects the rules that belong to classes alone.
Declaration
Section titled “Declaration”class Animal { legs: i32
fn init(self) { self.legs = 4 }
fn init(self, legs: i32) { self.legs = legs }
virtual fn sound(self) -> i32 { return 1 }
fn drop(self) { }}
class Dog : Animal { tail: i32
fn init(self, tail: i32) { // `Animal.init(self)`, the base `init` with no arguments, has already run here self.tail = tail }
override fn sound(self) -> i32 { return 2 }}- A
classhas single inheritance (class Dog : Animal) and a vptr;virtualmethods are replaced withoverride. - Field names follow
camelCase, type namesPascalCase. - A struct holds data only and has no methods; a class owns both. See interface and enum in the reference.
Constructors and destructors
Section titled “Constructors and destructors”- A class may declare
fn init(self, params...). Constructors take their parameters afterself, may be overloaded, and return nothing (E0722otherwise). - A class may declare at most one
fn drop(self). A destructor takes onlyself, returns nothing, and is neithervirtualnoroverride(E0722otherwise). T(args)builds a new object: it is zeroed, its vptr is set, theninitruns on it.- A class with an
initcannot be built with a field literal;T { ... }is thenE0723. A class without one keeps the field literal. - A derived
initruns the baseinitthat takes no arguments first. When the base has constructors but none without arguments, the derivedinitisE0722. - Inside an
init,self.f = von a droppable field is a raw store: the field holds zeroed memory there, so the old value is not dropped.
Drop and moves
Section titled “Drop and moves”A class needs drop when it has a drop method, or a field or base class that needs drop. A tuple needs drop when one of its elements does. Such values are move-only.
- A local or by-value parameter that needs drop is dropped when its block ends or the function returns, last-in first-out, together with
defer. - The drop runs the user
dropbody, then the drop of each field that needs drop in reverse declaration order, then the base class drop. A tuple drops its elements in reverse order. - Moving a droppable local by value (a
localinitializer, the right side of an assignment, a by-value argument, a tuple element, areturnvalue) is a move, and its drop is cancelled. A temporary passes its ownership on. - Moving a field, an element or
*pby value isE0720; take its address instead. - A class or tuple that needs drop cannot be a struct field or a global (
E0721); use a class field or a*Tmade withnew. An array whose element type needs drop is alsoE0721, because arrays do not drop their elements. A droppable element belongs in aVec<T>, which drops its elements when it is dropped. - A class with a vptr drops through it: slot 0 of every vtable holds the drop of the class itself (a C++ virtual destructor), so
delete pand__builtin_drop(p)through a base pointer run the drop of the real class. An@repr(c)hierarchy mirrors the C++ vtable and has no such slot. - A class without a vptr drops statically. Dropping through a base pointer when some derived class has its own drop would skip that drop, so it is
E0766: add avirtualmethod to the base, or drop through the derived type. __builtin_read(p)and__builtin_write(p, v)are the raw move and store through a pointer, with no move and no drop. They are what a container needs for its slots, and they are unchecked, like*p.
Allocation
Section titled “Allocation”The compiler knows no allocator type and no allocator method names, only the registered alloc and free functions.
new T(args)gets memory from the registeredallocand constructs in it.new(p) T(args)constructs in the memoryppoints at and allocates nothing (E0709whenpis not a pointer).delete pruns the drop of the pointee when it needs drop, then releases the memory through the registeredfree.- There is no explicit-allocator form of
delete. To release memory from another allocator, call__builtin_dropand that allocator’sfreeyourself. - Without a registered
alloc,new TisE0501; without a registeredfree,delete pisE0501. The stdlib registers both by default; see Runtime.
Generics
Section titled “Generics”A class may take type parameters (class Box<T> { ... }). Each use with concrete type arguments creates a separate class with its own layout and vptr. A generic method cannot be virtual or override (E0730) and cannot be overloaded (E0405).