Object
An object value is a dictionary of key-value pairs. They may have methods and getters that are able to access the object's properties and mutate them.
let alice be object with
name as "Alice", and
age as 30Definition
An empty object can be defined with just object as the value.
let empty_object be objectObjects can be defined with key-value pairs using object with ... syntax,
followed by a sequence of <property> as <value> pairs, following the Oxford comma rule.
let company be object with just ceo as "Pedro"
let alice be object with
name as "Alice",
age as 30, and
greet as function as
print "hello"Usage
Properties can be accessed and updated using the property access operator 's.
let alice be object with
name as "Alice", and
age as 30
print "My name is {alice's name}, and I'm {alice's age} years old."Properties can also be accessed with a string value using at.
let alice be object with
name as "Alice", and
age as 30
print alice at "name" # "Alice"New properties may be added to an object using extend ... with ... syntax.
extend alice with fav_color as "blue", and fav_number as 42
print alice's fav_color # "blue"Using for ... in ... do ... syntax, properties can be iterated over.
let alice be object with
name as "Alice", and
age as 30
for each property in alice's properties do
print "{property} is {alice at property}"Self-reference
Objects may reference themselves using own inside of a method. A method is a special
kind of function that is contextually bound to an object, and thus can safely access it.
let counter be object with
count as 0, and
increment as method as
update own's count to own's count plus 1
call counter's increment
print counter's count # 1When accessing a method field or passing it as a value, it becomes a regular function and own gets replaced with the original object.
# `own` here binds to the original object
let increment be counter's increment
# it's essentially equivalent to
let increment be function as call counter's incrementIt's possible to extend an object with a method, the own keyword in this context refers
to the object being extended.
let person be object with just name as "alice"
extend person with just
greet as method as
print "my name is {own's name}"The keyword own can be renamed using the as keyword and used inside of
a method.
let parent be object as mom with
name as "alice", and
child as object with
name as "bobby", and
get_parent_name as method as
return mom's nameUsing own inside of a renamed object is not allowed.
Limitations
A method field cannot be updated.
update counter's increment to "hello" # errorA method cannot be defined outside of an object.
let some_method be method as print "hello" # errorThe
ownkeyword cannot be used outside of a methodlet person be object with name as "alice", and greet as function as print "my name is {own's name}" # error
Getters
Returns the number of properties in the object. Note: methods and getters are not properties.
let alice be object with
name as "alice", and
age as 30
print alice's length # 2Returns an object's property names as a list of strings.
let alice be object with
name as "alice", and
age as 30
print alice's properties # list with name, and ageReturns an object's getter names as a list of strings.
let alice be object with
name as "alice",
age as 30, and
doubled_age as getter as age times 2
print alice's getters # list with just doubled_ageReturns an object's method names as a list of strings.
let alice be object with
name as "alice",
age as 30, and
greet as method as
print "hi i'm {own's name}"
print alice's methods # list with just greetMethods
Returns a string representation, e.g. "object with name as Alice, and age as 30", "object with just id as 42", or "object" if empty.
let alice be object with
name as "alice", and
age as 30
print alice's to_string # "object with name as alice, and age as 30"