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

impl Type { … } — grouping

impl-block ::= 'impl' generic-params? TypeExpr  '{' impl-item* '}'      // grouping
            |  'impl' generic-params? TypeExpr 'for' TypeExpr '{' trait-impl-item* '}'   // trait impl

The grouping form admits any declaration kind (impl-item); the trait-impl form is restricted to the contract member forms (trait-impl-item: fn / mutate / derive / check / query) and is governed by the conformance obligations of the trait surface and conformance (RFD 0026).

impl Person {
    pub fn greeting(self) -> String = "Hello, " + self.name;       // instance method
    pub fn adult(p: Person) -> Bool = p.age >= 18;                  // associated function
    pub rel ParentOf(parent: Person, child: Person) [1..1] [0..*];  // lexically grouped
    pub query oldest() -> Option<Person> {
        select p from p: Person order by p.age desc limit 1
    }
}

An impl Type block accepts any declaration kind: methods, associated functions, relations, queries, mutations, checks, associated type / const. All declarations inside the block are namespaced under Type::. Methods (items with self first parameter) are additionally accessible via UFCS dot-notation (alice.greeting()); associated functions and relations are reachable only as Type::name(args).

impl Person {
    /// Instance method (has `self`): both `alice.greeting()` and `Person::greeting(alice)` work.
    pub fn greeting(self) -> String = "Hello, " + self.name;

    /// Associated function (no `self`): reached as `Person::adult(p)`.
    pub fn adult(p: Person) -> Bool = p.age >= 18;

    /// Type-associated relation: reached as `Person::ParentOf(a, b)`.
    pub rel ParentOf(parent: Person, child: Person) [1..1] [0..*];

    /// Method-shaped query: `alice.oldest()` and `Person::oldest(alice)` both work.
    pub query oldest() -> Option<Person> {
        select p from p: Person order by p.age desc limit 1
    }
}

A relation declared inside impl Type is not exposed at module level. Choose the placement (module level, inside a topic mod, or inside impl Type) according to the semantic association the modeler intends — see Connecting concepts and relations.