Skip to content

16 · Reflection & Metaprogramming

Built-In Derives

Use @derive(Debug, Eq, Clone) to auto-generate standard trait implementations for a struct.

On this page

@derive(Debug, Eq, Clone) instructs the compiler to generate implementations of the named traits for the annotated type. The built-in derives are themselves written in Zolo via the same @derive_for mechanism — no privileged compiler path — so they serve as executable documentation for writing custom derives.

Apply @derive(Debug, Eq, Clone) to a User struct, then verify tostring, equality comparison, and clone all work without any hand-written impl.

11-derive-builtin.zolo
Playground
// Feature: `@derive(Trait)` generates trait impls at compile time. The built-in

// derives (Debug, Eq, Clone for structs; Eq/Clone/Hash/Ord for newtypes) are

// themselves written in Zolo via the same public `@derive_for` mechanism

// (dogfooded — no privileged compiler path).


@derive(Debug, Eq, Clone)
struct User { id: int, name: str }

let u = User.new(7, "ada")
print(tostring(u))
// expected: User { id: 7, name: ada }

print(u == User.new(7, "ada"))
// expected: true

let c = u.clone()
print(c == u)
// expected: true

Global index

Find your way through Zolo

Try an idea

Start here

9 results

9 results

en