Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Classes and structs

Dada programs declare classes and structs. Each declaration has a name, fields, and methods. Here is a simple struct Point with two Int fields:

classes_point_example [src]
crate::assert_ok!(
    r#"
        struct Point {
            x: Int;
            y: Int;
        }

        struct Main {
            fn test(given self) → Int {
                let p = new Point(22, 44);
                p.x→;
            }
        }
    "#
);

The entry type Main is special only by convention – the model checks every method. Each method has a receiver (self) with a permission (here, given, written given self) and a body that is type-checked against the declared return type.

New instances are created with new Point(22, 44), providing values for each field in declaration order. Fields are accessed with dot notation (p.x).

Declaration categories

Three declaration forms select a class predicate in the model:

DeclarationPredicateMeaning
class Foo { }ClassAffine data; may have a destructor; cannot be shared as a value
struct Foo { }StructShareable data with mutable fields; no destructor
shared struct Foo { }SharedCopyable when its generic arguments are shared; no destructor

Int is a built-in shared struct type, so its values can be freely copied. A plain class keeps ownership uniquely tracked and allows a destructor. Declaring a struct opts into sharing and rules out a destructor; declaring a shared struct also makes values copyable without an explicit sharing operation when all of its generic arguments are shared (automatically true with no arguments). The checker rejects nonempty drop bodies on either struct form.

We will return to class predicates as we explore the permission system.

Grammar

The core grammar uses ClassDecl for all three declaration categories:

ClassDecl [src]
#[term($class_predicate $name $binder)]
pub struct ClassDecl {
    pub name: ValueId,
    pub class_predicate: ClassPredicate,
    pub binder: Binder<ClassDeclBoundData>,
}

#[term($:where $,predicates { $*fields $*methods $?drop_body })]
pub struct ClassDeclBoundData {
    pub predicates: Vec<Predicate>,
    pub fields: Vec<FieldDecl>,
    pub methods: Vec<MethodDecl>,
    pub drop_body: DropBody,
}
/// Optional drop body for a class. When present, the statements are executed
/// when an owned handle to the class is dropped. Default is an empty block
/// (no drop body), which is semantically equivalent to `drop { }`.
#[term(drop $block)]
#[derive(Default)]
pub struct DropBody {
    pub block: Block,
}

The #[term(...)] attributes define the parsing grammar using formality-core conventions: $? is an optional element, $* means zero-or-more, $, means comma-separated, and $:where means the keyword where appears only if the list is non-empty. Binder introduces generic parameters that are in scope for the bound data.

Each field has a name and a type, and can optionally be declared atomic (which affects variance – more on this later):

FieldDecl [src]
#[term($?atomic $name : $ty ;)]
pub struct FieldDecl {
    pub atomic: Atomic,
    pub name: FieldId,
    pub ty: Ty,
}
Atomic [src]
#[term]
#[derive(Default)]
pub enum Atomic {
    #[default]
    #[grammar(nonatomic)]
    No,

    #[grammar(atomic)]
    Yes,
}

Only classes have destructors

A class may define a nonempty drop body. A struct or shared struct may not: independent sharing requires data without a destructor. Empty drop {} is represented identically to an absent body and is accepted for every category. These paired programs use the same body and differ only in declaration kind.

Property source and tests