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).
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.
fieldName.setVar(value); sets a default for
that field (visibility defaults to private).
fieldName.public(); or
fieldName.private(); sets whether the field
can be read from outside via
instanceVar.fieldName.
other.fieldName when
other is not the current
this (so one instance cannot read another’s
private fields). Inside methods, this.privateField
always works for the receiver.
constructorDefine, assign with
this.fieldName = expr; (use
math.add etc.—there is no
+ operator in expressions).
SPL does not have special get /
set keywords. Use methods:
methodDefine.getSecret(): return.number(this.secret); end;
this, e.g.
methodDefine.setSecret(x): this.secret = x; end;
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());
methodDefine.name(a, b): …
end; — parameters are identifiers. Return with
return.number /
return.string /
return.boolean.
Calling methods:
obj.methodName(arg1, arg2); — call on a
variable holding an instance (usual style after
class.createObj).
method.methodName(arg1, arg2); — implicit
this (inside constructor or another instance
method only).
obj.fieldName or
this.fieldName — read a field (no
parentheses). Private fields are only readable when
this is that instance (inside methods /
constructor).
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).
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.
Constructors and methods push a scope where this
is bound to the instance. See
Scoping for the
general model.
Filter the main table by Classes & OOP on the reference index.