13 · Decorators
Uniform deriving
Add a deriving suffix to structs and enums to request existing derive producers. The suffix uses the same validation and generated implementations as derive decorators. Unsupported traits or incompatible field types are diagnosed rather than synthesizing an unchecked implementation.
On this page
Add a deriving suffix to structs and enums to request existing derive producers. The suffix uses the same validation and generated implementations as derive decorators. Unsupported traits or incompatible field types are diagnosed rather than synthesizing an unchecked implementation.
Complete example
Uniform deriving.
uniform-deriving.zolo
// Uniform requests use the existing producer registry and real builtins.
@derive_for(Marker)
fn marker(info) -> Syntax<.Items> {
return quote items { impl ${info.ref} { fn marker(self) -> int { 37 } } }
}
@derive(Clone)
struct User { id: int, name: str } deriving Eq, Hash
let user = User { id: 1, name: "Ada" }
print(user.eq(User { id: 1, name: "Ada" }))
print(user.eq(User { id: 1, name: "Bea" }))
print(user.clone().name)
print(user.hash())
print(user.eq(User { id: 1, name: "Ada" }) && user.hash() == User { id: 1, name: "Ada" }.hash())
print(user.hash() != User { id: 2, name: "Ada" }.hash())
struct Counter(value: int) { fn read(self) -> int { self.value } } deriving Clone
print(Counter.new(7).clone().read())
enum State(Ready, Busy) deriving Eq, Marker
let state: State = .Ready
print(state.marker())
print(state.eq(State::Ready))
print(state.eq(State::Busy))
enum Shape { Empty, Point(int), Pair(int, str), Rect { width: int, height: int } } deriving Eq
print(Shape::Pair(7, "Ada").eq(Shape::Pair(7, "Ada")))
print(Shape::Pair(7, "Ada").eq(Shape::Pair(7, "Bea")))
print(Shape::Rect { width: 2, height: 3 }.eq(Shape::Rect { width: 2, height: 4 }))
struct Envelope<T: core::cmp::Eq + core::hash::Hash> { value: T } deriving Eq, Hash
let nested = Envelope { value: user }
print(nested.hash())
print(nested.eq(Envelope { value: User { id: 1, name: "Ada" } }))
struct Empty {} deriving Eq, Hash
print(Empty {}.hash())
struct Text { value: str } deriving Eq, Hash
print(Text { value: "ação" }.hash())
print(Text { value: "" }.hash())
newtype Id(int) deriving Eq, Hash
print(Id.new(8).hash())
print(Id.new(8).eq(Id.new(9)))
struct Defaults { count: int } deriving Default
print(Defaults.default().count)
Rules and behavior
Deriving and named tests · Documentation
From a checkout of the repository:
zolo examples/features/uniform-deriving.zolo
See also