Object
An Object represents 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 empty_object be objectObjects can be defined with key-value pairs after with, 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"The names of these user-defined fields may not be any of the built-in getters and methods of
the Object class, listed below.
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 existing object using extend.
extend alice with fav_color as "blue", and fav_number as 42
print alice's fav_color # "blue"As an Iterable, properties can be iterated over
using for each.
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
do 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 do 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}"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
Constructor
Creates a new Object copying the given object.
Getters
Returns an object's property names as a list of strings.
Returns an object's getter names as a list of strings.
Returns an object's method names as a list of strings.
A getter that returns an Iterator to iterate over the object properties.
Methods
Returns a string representation of the object, 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"Returns a string representation of the object in JSON format.
print "hello world"