(init params ..body) params: an arr body: zero or more forms
C
Defines an initializer method.
To construct an object, invoke its class as though calling a function: (Class a b)
.
This will...
- Allocate a new object which belongs to that class, with all of its fields set to
#n
and all of its states disabled. - Invoke the
init
method, passing ina
andb
as its arguments. - Automatically enable any toplevel
state*
forms. - Return the object.
The init
clause will automatically prepend field initializers to the beginning of its
method body:
- When a
field
clause has an explicit initializer form, that form is evaluated directly and assigned to those field(s). - When one of the
init
method's parameters is prefixed with@
, the value of that parameter is assigned to the corresponding field, which must not have an explicit initializer form.
Fields are always initialized in the same order that they were declared. This means that field-initializer forms can access fields defined earlier in the same class.
(defclass Example
(field a)
(field b (* @a 10))
(init (@a)
(prn b)))
; ...is equivalent to...
(defclass Example
(field a #n)
(field b #n)
(init (a)
(= @a a)
(= @b (* @a 10))
(prn b)))