LEVIATHAN v962456e · 962456eee1

Declarations

Classes

Declare reference types with fields, methods, constructors and accessors, with generics and inheritance from one or more base classes.

since 0.1.0-alpha.1linuxwindowswasm

Description

A class is a reference type: variables hold references to objects, an object has identity, and copying a variable copies the reference. Use a class for entities that have identity and mutable state; use a struct (see lang.structs) for plain data that should be copied.

class Name<T, U> : Base1, Base2 {
    ...members...
}
class Name;                          // a class with an empty body

A class may declare type parameters in angle brackets, and after the colon it lists its base classes and the interfaces it implements. Its body holds fields, methods, constructors, accessors and operators. A member can be declared inline with an access modifier (public int x;) or in sections that begin with public: or private:.

A class that derives from a base inherits the base's fields and methods. A method with the same name and parameter types as an inherited one overrides it, and a call through a variable of the base type runs the override.

A class, its subclass, and an override

class Animal {
public:
    string name = "animal";
    string speak() => name + " makes a sound";
private:
    int legs = 4;
public:
    int legCount() => legs;
}
class Dog : Animal {
    string speak() => name + " barks";
}
Dog d = Dog();
d.name = "Rex";
console.writeln(d.speak());
console.writeln(d.legCount());
Animal a = d;
console.writeln(a.speak());
Rex barks
4
Rex barks

a is declared as an Animal, but it refers to a Dog, so a.speak() runs the Dog version.

Rules

  • A class declares members between braces. class Name; declares a class with no members.
  • Type parameters follow the name in angle brackets: class Pair2<A, B> { ... }. Inside the class they can be used as types.
  • After a colon, a class lists its bases separated by commas. It may list several base classes (see lang.multiple-inheritance) and any number of interfaces (see lang.interfaces).
  • A method overrides an inherited method when its name and parameter types are the same. Calls dispatch on the runtime class of the object. It may be marked override; see lang.override.
  • Access is written inline (public int x;) or sectionally: the markers public: and private: apply to every member that follows them until the next marker. A member with no marker is public.
  • A field declared without an initializer gets its type's default value; see lang.fields.
  • A class that declares no constructor can still be constructed; see lang.constructors.

Examples

Generic classes and the empty-body form

class Pair2<A, B> {
    A first;
    B second;
    A getFirst() => first;
}
Pair2<int, string> p = Pair2(1, "x");
console.writeln(p.getFirst());
console.writeln(p.second);
class Marker;
Marker m = Marker();
console.writeln("made a marker");
1
x
made a marker

Notes

The compiler accepts private and records it, and documentation tools treat a private member as not part of a class's public surface, but code outside the class can currently still read and write a private member. Do not rely on private to enforce encapsulation.

See also

  • Interfaces — Declare the members a class must provide, including fields, and let any class satisfy them by declaring those members.
  • Multiple inheritance — Combine several base classes, how same-named fields and methods collapse or stay separate, and how to reach one base's member by qualification.
  • Value structs — Declare value types that are copied on every assignment, pass and return, with derived equality and explicit mutating methods.
  • Constructors — Declare constructors with new, select among them by label and argument types, call base constructors, and construct classes that declare none.
  • Fields — Declare the data a class or struct holds, with optional initializers, and how a field without an initializer gets its default value.