← SPL reference

Classes & object-oriented SPL

SPL supports user-defined classes with fields, methods, a constructor, optional single inheritance via another .spl file, and instance creation with class.createObj. use and inherit look for the named file next to the currently running .spl first, then under someProgrammingLanguage/lib/ (same dotted path rules as modules & imports).

1. Define a class

Use classDefine.ClassName():end;. The class name in the header is the name you pass to class.createObj.

classDefine.Box():
v.setVar(0);
v.public();
constructorDefine():
this.v = 0;
end;
methodDefine.get():
return.number(this.v);
end;
end;

this.fieldName = expr; is allowed in constructorDefine and in methodDefine (for encapsulated setters). There is no obj.fieldName = expr; syntax—only this can assign fields.

2. Fields & visibility

3. Getters & setters

SPL does not have special get / set keywords. Use methods:

From outside, call obj.getSecret(); / obj.setSecret(99);. Public fields can still be read directly as obj.v when marked v.public();.

classDefine.Counter():
n.setVar(0);
n.private();
constructorDefine():
this.n = 0;
end;
methodDefine.getN():
return.number(this.n);
end;
methodDefine.setN(x):
this.n = x;
end;
end;
class.createObj(c, Counter);
c.setN(7);
print.number(c.getN());

4. Methods

methodDefine.name(a, b):end; — parameters are identifiers. Return with return.number / return.string / return.boolean.

Calling methods:

5. Create objects

class.createObj(b, Box);
print.number(b.get());

First argument is the variable name (identifier or string); second is the class name as defined in classDefine. After class.createObj, call instance methods with obj.methodName(...) as above (not only method.*, which needs an active instance stack inside a constructor or method).

6. Inheritance

Inside classDefine, put inherit path.to.parent.spl; (same path style as use). The loaded file should define a class; that parent class is merged into the current class (fields and methods). Prefer one primary class per file used for inheritance so the merged parent is unambiguous.

7. Scoping

Constructors and methods push a scope where this is bound to the instance. See Scoping for the general model.

Reference

Filter the main table by Classes & OOP on the reference index.