-
Notifications
You must be signed in to change notification settings - Fork 0
Objects
Wi is an object-oriented programming language. But unlike languages like Java or C#, Wi uses concatenative prototype-based OOP.
Instead of classes, Wi has objects. You create an object, and then you can clone it to make new objects. You can also add new fields to objects, extending them with more functionality.
Objects, just like functions, are expressions. So to define an object you need to declare a variable and define it with the object expression:
var o = object {
};Objects are just bags of data. How do you define that data? You define fields:
var o = object {
x = 0;
};Next: Require
Now object o has a field called x with the value 0. To access that field, we use the field operator .:
var o = object {
x = 0;
};
o.x = 1;
print(o.x); // 1A method is a function associated with a specific object. To create a method, we simply define a function field:
var o = object {
my_method = function(self) {
};
};You may notice the parameter self - this is the object reference parameter. It's here so we can actually access the object's fields within its method.
Now, remember the invocation operator -> we talked about earlier? That's why it exists. If you access a method via the field operator . you need to pass self explicitly, but -> passes self implicitly!
o.my_method(o); // bad (explicit self)
o->my_method(); // good (implicit self)It's not only easier to write ->, it's also faster in execution.
We talked about how Wi relies on cloning, and we talked about the new operator earlier. Now it all comes together. But in case you forgot:
The new operator is used to clone an object. It creates a new object that is a copy of the original.
Here's an example:
var person = object {
name = "";
};
var alice = new person;
alice.name = "Alice";
var bob = new person;
bob.name = "Bob";
print(alice.name); // Alice
print(bob.name); // BobAnd of course, you can do that with methods too:
var person = object {
name = "";
greet = function(self) {
print("Hello " .. self.name .. "!");
};
};
var alice = new person;
alice.name = "Alice";
alice->greet(); // Hello Alice!
var bob = new person;
bob.name = "Bob";
bob->greet(); // Hello Bob!Does that look familiar? Yeah, it's the example from the Wi repository README.md!