x-lang

← Index

x/type/trait

Trait definition and predicate; the mixing happens in def-class.

Traits are composition for shared behaviour; delegates (a def-class

body form) is the storage-forwarding counterpart; interface remains

the abstract contract on an extends-chain – three distinct tools.

trait?

Test whether a value is a trait.

Parameters:

Returns: BOOL — #t for a trait value

See also: def-trait

def-trait

Define a trait: a named method bundle classes mix in with (with …).

(def-trait NAME (require SEL…) (method …)… (static (method …)…))

Body: (require SEL…) names the host chain must provide (checked at the

host’s def-class); (method …) forms mix into instance methods; a

(static …) block’s methods mix into statics. Bodies close over THIS

definition site; super inside them resolves against the HOST’s chain.

Mix in with (with NAME…) in a def-class body.

Examples:

(do (def-trait T (method hi (self) 'hi)) (def-class C () (with T)) ((new C) hi)) => 'hi

See also: def-class