Squirrel implements a class mechanism similar to languages like Java/C++/etc... however because of its dynamic nature it differs in several aspects. Classes are first class objects like integer or strings and can be stored in table slots local variables, arrays and passed as function parameters.
A class object is created through the keyword 'class' . The class object follows the same declaration syntax of a table(see tables) with the only difference of using ';' as optional separator rather than ','.
For instance:
class Foo { //constructor constructor(a) { testy = ["stuff",1,2,3]; } //member function function PrintTesty() { foreach(i,val in testy) { ::print("idx = "+i+" = "+val+" \n"); } } //property testy = null; }
the previous code examples is a syntactic sugar for:
Foo <- class { //constructor constructor(a) { testy = ["stuff",1,2,3]; testy = a; } //member function function PrintTesty() { foreach(i,val in testy) { ::print("idx = "+i+" = "+val+" \n"); } } //property testy = null; }
in order to emulate namespaces, is also possible to declare something like this
//just 2 regular nested tables FakeNamespace <- { Utils = {} } class FakeNamespace.Utils.SuperClass { constructor() { ::print("FakeNamespace.Utils.SuperClass") } function DoSomething() { ::print("DoSomething()") } } function FakeNamespace::Utils::SuperClass::DoSomethingElse() { ::print("FakeNamespace::Utils::SuperClass::DoSomethingElse()") } local testy = FakeNamespace.Utils.SuperClass(); testy.DoSomething(); testy.DoSomethingElse();
After its declaration, methods or properties can be added or modified by following the same rules that apply to a table(operator <- and =).
//adds a new property Foo.stuff <- 10; //modifies the default value of an existing property Foo.testy = "I'm a string"; //adds a new method function Foo::DoSomething(a,b) { return a+b; }
After a class is instantiated is no longer possible to add new properties or methods to it.
Note
Statics are read-only.class Foo { constructor() { //..stuff } name = "normal variable"; //static variable static classname = "The class name is foo"; };
class Foo </ test = "I'm a class level attribute" />{ </ test = "freakin attribute" /> //attributes of PrintTesty function PrintTesty() { foreach(i,val in testy) { ::print("idx = "+i+" = "+val+" \n"); } } </ flippy = 10 , second = [1,2,3] /> //attributes of testy testy = null; }Attributes are, matter of fact, a table. Squirrel uses </ /> syntax instead of curly brackets {} for the attribute declaration to increase readability.
This means that all rules that apply to tables apply to attributes.
Attributes can be retrieved through the built-in function classobj.getattributes(membername) (see built-in functions). and can be modified/added through the built-in function classobj.setattributes(membername,val).
the following code iterates through the attributes of all Foo members.
foreach(member,val in Foo) { ::print(member+"\n"); local attr; if((attr = Foo.getattributes(member)) != null) { foreach(i,v in attr) { ::print("\t"+i+" = "+(typeof v)+"\n"); } } else { ::print("\t<no attributes>\n") } }
The class objects inherits several of the table's feature with the difference that multiple instances of the same class can be created. A class instance is an object that share the same structure of the table that created it but holds is own values. Class instantiation uses function notation. A class instance is created by calling a class object. Can be useful to imagine a class like a function that returns a class instance.
//creates a new instance of Foo local inst = Foo();
When a class instance is created its member are initialized with the same value specified in the class declaration.
When a class defines a method called 'constructor', the class instantiation operation will automatically invoke it for the newly created instance. The constructor method can have parameters, this will impact on the number of parameters that the instantiation operation will require. Constructors as normal functions can have variable number of parameters (using the parameter ...).
class Rect { constructor(w,h) { width = w; height = h; } x = 0; y = 0; width = null; height = null; } //Rect's constructor has 2 parameters so the class has to be 'called' //with 2 parameters local rc = Rect(100,100);
After an instance is created, its properties can be set or fetched following the same rules that apply to tables. Methods cannot be set.
Instance members cannot be removed.
The class object that created a certain instance can be retrieved through the built-in function instance.getclass()(see built-in functions)
The operator instanceof tests if a class instance is an instance of a certain class.
local rc = Rect(100,100); if(rc instanceof ::Rect) { ::print("It's a rect"); } else { ::print("It isn't a rect"); }
Squirrel's classes support single inheritance by adding the keyword extends, followed by an expression, in the class declaration. The syntax for a derived class is the following:
class SuperFoo extends Foo { function DoSomething() { ::print("I'm doing something"); } }
When a derived class is declared, Squirrel first copies all base's members in the new class then proceeds with evaluating the rest of the declaration.
A derived class inherit all members and properties of it's base, if the derived class overrides a base function the base implementation is shadowed. It's possible to access a overridden method of the base class by fetching the method from the base class object.
Here an example:
class Foo { function DoSomething() { ::print("I'm the base"); } }; class SuperFoo extends Foo { //overridden method function DoSomething() { //calls the base method ::Foo.DoSomething(); ::print("I'm doing something"); } }
Same rule apply to the constructor. The constructor is a regular function (apart from being automatically invoked on contruction).
class Base { constructor() { ::print("Base constructor\n"); } } class Child extends Base { constructor() { ::Base.constructor(); ::print("Child constructor\n"); } } local test = Child();
The base class of a derived class can be retrieved through the keyword parent. parent is a 'pseudo slot'. The parent slot cannot be set.
local thebaseclass = SuperFoo.parent;
Note that because methods do not have special protection policies when calling methods of the same objects, a method of a base class that calls a method of the same class can end up calling a overridden method of the derived class.
class Foo { function DoSomething() { ::print("I'm the base"); } function DoIt() { DoSomething(); } }; class SuperFoo extends Foo { //overridden method function DoSomething() { ::print("I'm the derived"); } function DoIt() { ::Foo.DoIt(); } } //creates a new instance of SuperFoo local inst = SuperFoo(); //prints "I'm the derived" inst.DoIt();
Class instances allow the customization of certain aspects of the their semantics through metamethods(see Metamethods). For C++ programmers: "metamethods behave roughly like overloaded operators". The metamethods supported by classes are _add, _sub, _mul, _div, _unm, _modulo, _set, _get, _typeof, _nexti, _cmp, _call, _delslot,_tostring
Class objects instead support only 2 metamethods : _newmember and _inherited
the following example show how to create a class that implements the metamethod _add.
class Vector3 { constructor(...) { if(vargc >= 3) { x = vargv[0]; y = vargv[1]; z = vargv[2]; } } function _add(other) { return ::Vector3(x+other.x,y+other.y,z+other.z); } x = 0; y = 0; z = 0; } local v0 = Vector3(1,2,3) local v1 = Vector3(11,12,13) local v2 = v0 + v1; ::print(v2.x+","+v2.y+","+v2.z+"\n");
Since version 2.1, classes support 2 metamethods _inherited and _newmember. _inherited is invoked when a class inherits from the one that implements _inherited. _newmember is invoked for each member that is added to the class(at declaration time).