Defines a struct.
defstruct
is syntax sugar for defclass
. It defines a class with all of
the specified fields, along with:
- Implementations for the methods
op-clone
andop-eq?
. - A function bound to the global variable
StructName:new
, which instantiates the struct using positional arguments. - A macro bound to the global variable
StructName
, which instantiates the struct using named arguments, similar to thetab
macro. - A function bound to the global variable
StructName?
, which tests whether or not a value is an instance of this struct.
To keep structs simple, their class clauses may only be const
,
met
or prop
. Classmacros, mixins, states, wrappers,
field patterns and field initializers are not supported.
(defstruct Rect
x y w h
(met area ()
(* @w @h)))
(let r (Rect:new 0 0 10 10))
(let s (Rect
(x 2)
(y 2)
..r))
(prn (.area s)) ; prints 100
(prn (Rect? s)) ; prints #t
(let t (clone s))
(prn (eq? s t) (same? s t)) ; prints #t #f