Traits¶
Mighty traits group related behavior behind a shared method set, analogous to Rust traits or interfaces.
Declaration¶
A trait is declared at the top level. Each method must specify its signature; bodies (default methods) are reserved for a later slice.
Implementation¶
The compiler enforces coherence: at most one
impl Trait for Type per (trait, type) pair. A duplicate raises
MT4022 trait_coherence_violation.
Dispatch¶
When you call x.hash():
- Inherent first. If
impl T { fn hash(self) -> U64 }exists forT, it wins regardless of trait impls. - Trait fallback. If exactly one trait in scope provides
hashforT, that impl dispatches. Two or more in scope: ambiguous,MT4020 method_ambiguous— disambiguate by importing fewer traits or using an explicit trait-qualified call (slice 5 doesn't yet parse that syntax; the diagnostic lists candidates). - None of the above.
MT4021 method_not_found.
dyn Trait¶
A dyn Trait value erases the static type and dispatches
dynamically:
trait Show {
fn show(self) -> Str
}
impl Show for I32 {
fn show(self) -> Str { "<int>" }
}
fn print(s: dyn Show) -> Unit {
log(s.show())
}
Slice 5 keeps object-safety conservative: a trait used through dyn
may not mention Self in any method parameter or return, and may not
have method-level generics. Violation: MT4023 dyn_requires_object_safe.
#[derive(Copy, Hash, Eq)]¶
The derive attribute generates conformance to a trait without writing
the body. Slice 5 supports Copy, Hash, and Eq:
Copy validates every field's type is itself Copy
(MT4040 derive_copy_field_not_copy otherwise). Hash / Eq register
synthetic impls so dyn Hash = my_t resolves.
A shorthand derive Copy struct Vec2 { ... } is also accepted.
Unknown derive names raise MT4041 derive_unknown.
Try it¶
There is no single 15_traits.mty example — trait declarations and
impls appear across most of the larger examples (19_backend_service,
20_frontend_component). Paste any snippet from this chapter into
scratch.mty and run:
The conformance corpus at
tests/conformance/traits/
exercises the full dispatch + coherence machinery.
End of the tour¶
You've reached the end of the chapter-by-chapter tour. Next steps:
- Read the normative spec at
docs/spec/v1.0-rc.md. - Browse the CLI reference.
- Skim the FAQ.
- Open an issue on github.com/hassard0/Mighty if you find anything that surprises you.