Object
Moscrif Object is declared as class containing properties and methods. In run-time, Object is represented as an instance of a class constructed by constructor.
Static properties and Constants are defined at the beginning of class. Constructor in Moscrif is a function with reserved name this(). Constructor can have parameters or can be without parameters. We advise you to use non-parametric constructors and create class factories instead.
Example of singleton in Mosrif:
class A
{
var instance; function this()
{
assert A.instance == null;
A.instance = this;
}
} var a = new A();
var b = A.instance; console <
console <
Class extension in Mosrif is done by colon (:) in class definition. See how we create new class B extended from class A, previously declared. See the constructor of the class B.
class B : A {
//class definition goes her
}
If class B does not have specified constructor; it is used from class A. See what happens when we have own constructor.
class B : A
{
function this()
{
//constructor definition goes here
}
} var c = new B();
var d = c.instance; console <
console <
As you can see, type of d is not an object,is undefined. This happened because we did not call a parent constructor. Let’s do our example again with parent constructor call.
class B : A
{
function this()
{
//call parent constructor
super(); //constructor definition goes here
}
}
Here we make simple class factory. Factory is a method that returns an instance of called class. Inside of the static factory method we create instance of the object and we can set any attribute of the object and return it.
class A
{
var _instance = null; function this()
{
} //class factory
property instance(v)
{
get
{
if(this._instance == null)
this._instance = new A(); return this._instance;
}
} property value(v)
{
get return this._value;
set this._value = v;
} } A.instance.value = "Class Factory test"
var o = A.instance;
console <
As you can see Moscrif invents a property definition. Each property has its own getter and setter. It is quite handy to have property defined in this way. Other developer languages (such as Java) define getter and setter as methods of the class.
There are many properties and methods applied to each Object in Moscrif. For example clone of the instance, toString, valueOf or call.
Here we have a look at call method. This method is used if the name of the method is a string or part of object and we would like to execute it. Paramaters; if applicable, are defined as Array. This feature can only function with non-optional parameters. If an optional parameter is defined, method must be set in parameters array.
class Foo { function this()
{ } function callMethodA()
{
console <<"Method A is called"<<"\n";
} function callMethodB(a,b,c)
{
console <<"Method B is called, with patameters("+a+","+b+","+c+")"<<"\n";
} } var str1 = "callMethodA";
var str2 = "callMethodB";
var obj = new Foo();
var arg = new Array("a","b","c"); //call method A using class name
Foo.call(symbol(str1),new Array()); //call method B using instance of object
obj.call(symbol(str2),arg);