-
-
Notifications
You must be signed in to change notification settings - Fork 4
constructor
There is a subtle difference between
person{name:"Joe" address{street:"no way 123"}}
person:{name="Joe" address:{street="no way 123"}}
While both construct an object person, the variant with colon constructs pure data while the variant without colon constructs the person via a constructor, if available.
class person{
init{
id=random()
}
}
a=person{} // preferred way
a=person() // works as constructor unless person is a function
By default classes are extendible, so
person{name:"Joe" address{street:"no way 123"}}
works as expected: the person object now has a name field and an id field via the init
constructor.
There are situations where objects are meant to be immutable or non-extendible, or where successful construction depends on the right data at initialization time. These required fields can be denoted via exclamation mark:
class person{
name!
address
}
Now every construction person{name:"blah"} must provide the name argument/field property.