Traits

A trait names a static method contract. Matching methods don't imply conformance, so a type must declare the promise explicitly.

Unlike object-oriented interfaces or Rust trait objects, Moth traits don't create runtime values or dynamic dispatch. Generic bounds require conformance evidence for each concrete argument type during compilation. See Evidence travels with values.

Choose the explanation level: Trait declarations

Trait declarations

A trait names a behaviour that a type can promise to provide.

A trait with one requirement

DISPLAY_TEXT must:
    display |This| -> String
;

This says: any type that claims to be DISPLAY_TEXT must provide a display method that returns a String.

A marker trait

SERIALIZABLE must:
;

A marker trait has no methods. It is still a real contract that a type can explicitly adopt.

A trait forms an explicit nominal method contract. A type satisfies a trait only when source declares conformance.

Trait declaration syntax

DISPLAY_TEXT must:
    display |This| -> String
;

Exact trait declaration contract

  • A trait is an explicit nominal method contract.
  • Trait names use all-caps identifiers.
  • Trait declarations are top-level.
  • TRAIT must: opens a trait declaration.
  • One final semicolon closes the declaration.
  • An inline trait closes on the line containing its must:. A multiline trait closes with ; alone on its own line.
  • Only whitespace or a -- comment may follow the declaration's closer on its line.
  • Requirements are method signatures only.
  • Marker traits with no requirements are valid.
  • Traits are compile-time static contracts.
  • Traits are not runtime values.
  • Static non-method requirements are deferred.
  • Generic traits and generic trait methods are outside scope.

Declaration placement follows Blocks and statements.

Marker traits

SERIALIZABLE must:
;

A marker trait carries no requirements but still represents a deliberate contract that a type may declare.

Traits are not value types

A trait name may appear in trait declarations, conformance declarations and generic bounds. It is invalid as an ordinary variable, parameter, field, return, collection element, choice payload or alias target type.

Concepts related to trait declarations

Choose the explanation level: Trait requirements

Trait requirements

Trait requirements describe the methods a conforming type must have. The receiver has a special name.

This means the implementing type

DISPLAY_TEXT must:
    display |This| -> String
;

This is a placeholder for whatever type later declares conformance.

Mutable receiver

RESETTABLE must:
    reset |~This|
;

~This means the method may mutate the receiver.

Named This parameters

SAME must:
    equals |This, other This| -> Bool
;

When This is not the receiver, it must have a parameter name like other This.

Trait requirements describe the method shapes a conforming type must provide. The receiver is always This or ~This.

Receiver syntax

RESETTABLE must:
    reset |~This|
;

COMPARABLE_WITH_SELF must:
    same |This, other This| -> Bool
    clone_value |This| -> This
;

Exact trait requirement contract

  • Requirement receivers use This or ~This.
  • Lowercase this is source-method syntax and invalid in a requirement.
  • Bare This and ~This are valid only as the first receiver parameter.
  • This is a shared receiver.
  • ~This is a mutable receiver.
  • Direct non-receiver This parameters must be named, such as other This.
  • Direct return This is supported.
  • Composed This forms such as This?, {This} and Box of This are rejected in the Alpha surface.
  • This is trait-local and invalid outside trait declarations.
  • Requirements have no body.
  • Parameter names are not part of the later conformance shape.
  • Parameter access modes, types, return types and return channels are part of the shape.
  • The whole return contract is part of the shape. A requirement declared -> ! matches only another -> ! contract. An ordinary return contract does not match Never, and Never does not match an ordinary return.

Multiple requirements

Multiple method requirements may appear in one trait. The current separator between requirements is a newline. Each requirement has no body and no standalone semicolon. The trait itself closes with a final semicolon, inline on the line containing its must: or alone on its own line. See Blocks and statements.

NAMED must:
    name |This| -> String
    id |This| -> I32
;

Non-receiver This

SAME must:
    equals |This, other This| -> Bool
;

This as a non-receiver parameter must carry a name.

Concepts related to trait requirements

Choose the explanation level: Conformance

Conformance

A type does not satisfy a trait because it happens to have the right methods. It must say so explicitly.

Method plus conformance

DISPLAY_TEXT must:
    display |This| -> String
;

Label = |
    text String,
|

display |this Label| -> String:
    return this.text
;

Label must DISPLAY_TEXT

Label must DISPLAY_TEXT is the explicit promise. Without it, the compiler does not treat Label as DISPLAY_TEXT.

Shape must match exactly

The method must have the same receiver mutability, parameter types, return type and return channel. Parameter names do not matter.

Only same-file nominal types

You can conform structs, choices and aligned generic constructors declared in the same file. You cannot conform builtins, imported types or types from another file.

A conformance declaration states that a type implements one or more traits. Matching method shapes alone are not conformance.

Conformance syntax

Label = |
    text String,
|

display |this Label| -> String:
    return this.text
;

Label must DISPLAY_TEXT

Exact conformance contract

  • Conformance is explicit.
  • A matching method without Type must TRAIT is not conformance.
  • A conformance declaration is top-level.
  • It is bodyless.
  • It is newline-terminated and has no semicolon.
  • Several traits may be listed with commas.
  • Every requirement must resolve to a method with exact:
    • receiver mutability
    • non-receiver parameter access mode
    • parameter types
    • return types
    • return channels
    • whole return contract, so -> ! matches only -> !
  • Parameter names do not affect conformance.
  • Accepted user-authored targets are same-file structs, choices and aligned generic constructors.
  • Rejected targets include builtins, imported types, dependency package types, external opaque types and nominal types declared in another file.
  • Canonical evidence travels with values of the target type, so a generic call need not name the target type or the trait.
  • Conformance does not independently bind receiver methods.
  • Concrete values use the receiver methods carried by their type.
  • Specialise-by-concrete-instance conformance is outside scope.

Implicit numeric failure cannot secretly widen a trait requirement or generic conformance. The requirement's declared typed error slot stays exact. A private method with no error slot may still infer and propagate that failure within its module; resolved evidence includes the concrete failure in the enclosing body's analysis. An exported method without an error slot must discharge it. Private propagation does not add an unlisted trait return channel. See Returns and multiple values for the contract.

Multiple traits

Label must DISPLAY_TEXT, SERIALIZABLE

Aligned generic constructor conformance

A generic type declared in the same file may declare conformance. The trait requirements use This. The source implementation uses the concrete receiver type and lowercase this.

Box type A = |
    value A,
|

DISPLAY_TEXT must:
    display |This| -> String
;

display type A |this Box of A| -> String:
    return [: box]
;

Box must DISPLAY_TEXT

Rejected targets

-- Error: builtin types cannot declare user-authored conformance
I32 must CASTABLE_TO_STRING

Use a local wrapper choice or struct when adapting a value owned by another file or package.

Concepts related to conformance

Choose the explanation level: Generic trait bounds

Generic trait bounds

A generic bound says: this placeholder type must provide a specific trait.

One bound

DISPLAY_TEXT must:
    display |This| -> String
;

render type Item is DISPLAY_TEXT |item Item| -> String:
    return item.display()
;

Inside render, Item is known to have a display method.

Several bounds

NAMED must:
    name |This| -> String
;

render_named type Item is DISPLAY_TEXT and NAMED |item Item| -> String:
    return item.display()
;

and adds another bound to the same parameter.

Generic bounds use traits to promise that a generic parameter provides certain behaviour inside the generic declaration.

Generic trait bound syntax

DISPLAY_TEXT must:
    display |This| -> String
;

render type Item is DISPLAY_TEXT |item Item| -> String:
    return item.display()
;

Exact generic trait bound contract

  • Generic bounds use is, not must.
  • and adds bounds to one parameter.
  • Commas separate generic parameters.
  • Bounds apply to generic functions, structs and choices.
  • Bounds on a public generic (function, struct, choice or receiver method) must name a trait public at its owner: exported by the declaring module root, directly or by re-export, or supplied by a provider's public interface.
  • The publishing module need not re-export an imported trait. A trait private at its owner is rejected with MOTH-RULE-0074.
  • Each concrete argument requires reusable evidence for its type.
  • Trait names are static contracts, not concrete type arguments.
  • A bound may enable a unique required receiver call inside the generic declaration.
  • Bound calls resolve statically to concrete source methods.
  • Concrete values use the receiver methods carried by their type.
  • Conformance does not bind methods into scope.
  • Backends do not receive trait objects or runtime evidence tables.
  • where syntax is rejected.
  • File-local evidence-backed dispatch outside reusable conformance is outside scope.

Generic dispatch preserves the requirement's declared error contract. It cannot secretly widen an error-free requirement. A concrete private method may still propagate inferred failure inside its module through resolved evidence. An exposed method must discharge that failure at the declared boundary. See Conformance and Progress.

Multiple bounds on one parameter

NAMED must:
    name |This| -> String
;

render_named type Item is DISPLAY_TEXT and NAMED |item Item| -> String:
    return item.display()
;

Multiple parameters with bounds

render_pair type A is DISPLAY_TEXT, B is DISPLAY_TEXT |left A, right B| -> String:
    return [left.display(), right.display()]
;

Evidence travels with values

Concrete calls and instantiations require reusable evidence. User-authored reusable evidence originates from the canonical conformance declared with the same-file target type. Callers reuse that evidence wherever a value of the type reaches a file, without naming the type or the trait.

Compiler-owned evidence also exists for the builtin cast table.

Label = |
    text String,
|

display |this Label| -> String:
    return this.text
;

Label must DISPLAY_TEXT

value = render(Label("ok"))

Concepts related to generic trait bounds

Choose the explanation level: Trait incompatibility

Trait incompatibility

Some trait contracts are mutually exclusive. A type cannot claim both.

Two ways to produce text

TEXT must:
    text |This| -> String
;

FALLIBLE_TEXT must:
    try_text |This| -> String, Error!
;

TEXT must not FALLIBLE_TEXT

TEXT promises a String directly. FALLIBLE_TEXT promises a String or an error. A type cannot be both at once.

Symmetric

The relation goes both ways. Saying A must not B also means B must not A in effect.

Trait incompatibility records that two traits cannot both be claimed by the same concrete type.

Trait incompatibility syntax

TEXT must:
    text |This| -> String
;

FALLIBLE_TEXT must:
    try_text |This| -> String, Error!
;

TEXT must not FALLIBLE_TEXT

Exact trait incompatibility contract

  • Incompatibility is trait metadata.
  • It is symmetric.
  • A concrete type cannot explicitly conform to both sides.
  • Several incompatible traits may be listed.
  • It affects conformance validation only.
  • It does not:
    • add requirements
    • imply negative conformance
    • create trait composition
    • create inheritance
    • affect runtime dispatch

Compiler-owned pairs

The compiler uses must not for incompatible core cast trait pairs. For example, CASTABLE_TO_STRING and TRY_CASTABLE_TO_STRING cannot both be claimed for the same source type. The conforming type is the cast source type. The trait name identifies the cast target.

Concepts related to trait incompatibility

Choose the explanation level: Core cast traits

Core cast traits

The compiler owns a fixed set of traits for user-defined cast sources. Conformance proves that a type can reach a target through plain cast.

Label = |
    text String,
|

to_string |this Label| -> String:
    return this.text
;

Label must CASTABLE_TO_STRING
value String = cast Label("docs")

Fifteen target families support source-authored evidence: I8, I16, I32, I64, U8, U16, U32, U64, F16, F32, F64, Bool, String, Char and Error. Each target has infallible and fallible evidence. A type cannot declare both for the same target.

Fallible evidence returns the target and builtin Error!. Its caller still uses plain cast; ordinary error handling delivers the actual failure. Byte and Dec keep builtin conversion evidence but have no source-authored traits. See Core cast traits for every signature.

The compiler owns a closed set of static cast traits. They provide source evidence for the plain cast expression.

Trait families

Each of fifteen target families has an infallible and a fallible trait. The targets comprise all eleven fixed numeric types plus Bool, String, Char and Error:

  • I8: CASTABLE_TO_I8 requires to_i8 |This| -> I8. TRY_CASTABLE_TO_I8 requires try_to_i8 |This| -> I8, Error!.
  • I16: CASTABLE_TO_I16 requires to_i16 |This| -> I16. TRY_CASTABLE_TO_I16 requires try_to_i16 |This| -> I16, Error!.
  • I32: CASTABLE_TO_I32 requires to_i32 |This| -> I32. TRY_CASTABLE_TO_I32 requires try_to_i32 |This| -> I32, Error!.
  • I64: CASTABLE_TO_I64 requires to_i64 |This| -> I64. TRY_CASTABLE_TO_I64 requires try_to_i64 |This| -> I64, Error!.
  • U8: CASTABLE_TO_U8 requires to_u8 |This| -> U8. TRY_CASTABLE_TO_U8 requires try_to_u8 |This| -> U8, Error!.
  • U16: CASTABLE_TO_U16 requires to_u16 |This| -> U16. TRY_CASTABLE_TO_U16 requires try_to_u16 |This| -> U16, Error!.
  • U32: CASTABLE_TO_U32 requires to_u32 |This| -> U32. TRY_CASTABLE_TO_U32 requires try_to_u32 |This| -> U32, Error!.
  • U64: CASTABLE_TO_U64 requires to_u64 |This| -> U64. TRY_CASTABLE_TO_U64 requires try_to_u64 |This| -> U64, Error!.
  • F16: CASTABLE_TO_F16 requires to_f16 |This| -> F16. TRY_CASTABLE_TO_F16 requires try_to_f16 |This| -> F16, Error!.
  • F32: CASTABLE_TO_F32 requires to_f32 |This| -> F32. TRY_CASTABLE_TO_F32 requires try_to_f32 |This| -> F32, Error!.
  • F64: CASTABLE_TO_F64 requires to_f64 |This| -> F64. TRY_CASTABLE_TO_F64 requires try_to_f64 |This| -> F64, Error!.
  • Bool: CASTABLE_TO_BOOL requires to_bool |This| -> Bool. TRY_CASTABLE_TO_BOOL requires try_to_bool |This| -> Bool, Error!.
  • String: CASTABLE_TO_STRING requires to_string |This| -> String. TRY_CASTABLE_TO_STRING requires try_to_string |This| -> String, Error!.
  • Char: CASTABLE_TO_CHAR requires to_char |This| -> Char. TRY_CASTABLE_TO_CHAR requires try_to_char |This| -> Char, Error!.
  • Error: CASTABLE_TO_ERROR requires to_error |This| -> Error. TRY_CASTABLE_TO_ERROR requires try_to_error |This| -> Error, Error!.

The list gives requirement signatures. A source implementation uses an ordinary lowercase this receiver and its concrete type.

Contract

  • These traits resolve without dependency clauses.
  • They cannot be redeclared, imported, exported, aliased or shadowed, and they cannot be used as values.
  • The compiler registers builtin conversion evidence, including for Byte and the Dec family.
  • Source-authored traits cover only the fifteen targets above. Byte and Dec have no source-authored trait families.
  • Same-file nominal source types and aligned generic nominal constructors can provide evidence with explicit conformance.
  • Matching method shape alone does not prove conformance.
  • Infallible and fallible evidence for the same source type and target are mutually exclusive.
  • Infallible methods return the target. Fallible methods return the target and builtin Error!.
  • Evidence follows the complete static source and target types, not known values or optimiser proofs.
  • Evidence resolves before HIR. The traits prove source evidence only and add no general conversion framework.

Infallible evidence

Label = |
    text String,
|

to_string |this Label| -> String:
    return this.text
;

Label must CASTABLE_TO_STRING

value String = cast Label("docs")

The compiler checks that an infallible evidence method does not acquire a failure contract.

Fallible evidence

A fallible method returns through builtin Error!. Its caller uses the same plain cast syntax:

Label = |
    text String,
|

try_to_i32 |this Label| -> I32, Error!:
    value I32 = cast this.text
    return value
;

Label must TRY_CASTABLE_TO_I32

parsed I32 = cast Label("42") catch |err|:
    then 0
;

The method's typed error follows ordinary call and catch compatibility rules. Builtin conversion failures use implicit failure. A custom error still needs explicit mapping where the destination expects builtin Error.

There is no cast-specific propagation marker. Postfix ! remains the normal propagation form for a fallible call, not a cast.

Concepts related to core cast traits

Choose the explanation level: Trait design scope

Trait design scope

Traits are for static reuse, not runtime flexibility.

Static reuse

render type Item is DISPLAY_TEXT |item Item| -> String:
    return item.display()
;

The compiler knows at compile time which concrete display method to call.

Runtime heterogeneity with choices

When you need different types at runtime, use a choice instead.

Renderable ::
    LabelItem | value Label |,
    ButtonItem | value Button |,
;

A choice is a tagged union. The variant tag decides which code to run.

Moth traits stay static. They name behaviour before runtime begins and stay out of the type system as runtime values.

Static model

Trait names are valid in:

  • trait declarations
  • conformance declarations
  • generic bounds

Trait names are invalid as ordinary:

  • variables
  • parameters
  • fields
  • returns
  • collection elements
  • map types
  • choice payloads
  • alias targets

Use a generic bound for static reuse. Use a choice for runtime heterogeneity.

-- Static reuse
render type Item is DISPLAY_TEXT |item Item| -> String

-- Runtime heterogeneity
Renderable ::
    LabelItem | value Label |,
    ButtonItem | value Button |,
;

Static traits are backend-independent. Backends do not receive trait objects, erased dispatch or runtime evidence tables.

Deferred

  • static non-method requirements
  • additional compiler-owned builtin conformance families beyond the current closed core-cast evidence table
  • broader standard trait taxonomy that does not create runtime trait values

Outside scope

  • default methods
  • associated types and constants
  • inheritance
  • trait aliases and composition
  • generic traits and generic methods
  • blanket, conditional, negative or specialised conformance
  • structural conformance
  • dynamic trait values and trait objects
  • downcasting and reflection
  • output coercion
  • operator integration
  • automatic primitive conformance
  • derive-style hashing, ordering, formatting, iteration or serialization families

Do not treat compiler-owned test-only or scaffold traits as public language features.

Concepts related to trait design scope