Class
Last updated
Was this helpful?
Last updated
Was this helpful?
You may know what a class is, a "blueprint" for which an object is to be constructed from, but internally they are very simple (the source code contains detailed information of their implementation, may provide additional details). A class has 3 main parts:
A class is a scope. An instance of a class is really just a scope. This may sound confusing but, think about what a scope is, it is a hashmap of variables which can inherit and provides interfacing for accessing it's variables. That sounds just like what a class is.
So what else? A class has an initializer, this is the main part seperating a class from a scope. Here are the steps taken during a class initalization:
Create an empty scope (this is called the "instance")
Copy the class's scope to the instance (and the inheriting class's if applicable).
Run the initializer (init
) and pass the instance as "self".
Return the instance.
As you can see, the returned instance, is just a regular scope, but pre-populated. When executed, methods are passed the scope as a whole. Here's an example:
as you can see, whatIsSelf
is passed its scope (A
) by the evaluator, but the scope is not directly attached to whatIsSelf
.