The Defacto Defaults of the Mynx Class Default Method
Concept of Default Method
A Mynx class can specify a default method--a method that is invoked implicitly on an instance, when the instance is used--like a constructor or destructor.
For example:
x = y;
By operator overloading, the = operator is the method 'set':
x.set(y);
The variable y is an instance of some class, but it would invoke a default method if one is declared in the class. Implicitly, all instances return a reference to the instance, or something like:
public myClass me is
return this;
end me;
However no
me method is actually defined, but it is equivalent in operation. So in effect, all instances have a default method. Thus the code could be re-written in the most explicit syntax of:
x.set(y.me);
Syntax of a Default Method
Originally a default method had the BNF of:
DEFAULT := ACCESS default is to [null|ID] ';'
But after some consideration and contemplation with use cases, it seems inefficient for two reasons:
- The default method is invoked implicitly, so is always visible thus the access is always public.
- The default method of being to null is a do nothing operation, but that is contradictory to even the implicit default method which returns the instance reference.
Altering the sentence prefix and suffix for partitioning (the compiler phase before syntax analysis in sentential parsing) and the parser for the default method sentence, the syntax in the revised Extended Backus-Naur Form (EBNF) for Mynx is:
DEFAULT := default is to ID ';'
Unlike a destructor which can be private or protect in visibility, the default method if defined is visible--it cannot be invoked explicitly, so must be visible to invoke it exists. The to null is removed, but if a software developer's intention is to do nothing, the method it is referring to can do nothing.
Some other semantic rules for a default method are:
- The default method cannot be static - must be a stateful method, an instance method.
- The default method is a function not a procedure; the default method returns something.
- The default method must be public method in the class to avoid implicit circumvention of the external class method visibility.
Example of Use of a Default Method
A default method is primarily syntactic sugar, i.e. it is used to make code more clean and elegant (if such a thing as elegance is possible...). For example, for a two-dimensional array with the following expression-assignment statement:
x(2,3) = x(3,2);
The default method is the
at method, which returns the particular type stored at that position. Substituting the explicit for the implicit method call:
x.at(2,3) = x.at(3,2)
And the operator
= is overloaded in the Int class as the set method. Substituting that in place of the operator:
x.at(2,3).set(x.at(3,2));
The original code is syntactically neat and clean, whereas the actual full method invocation expression is not as elegant.
A class
Array2DInt would define a method in a class:
constant class Array2DInt is
public constant Int at(in Ordinal x, in Ordinal y) is
!! define the method !!
end at;
public default is to at;
end class;
Conclusion
The default method of a class has specific semantics, but is like an operator overload, association of an operator with a method. In the case of a default method, it is overloading a class instance for a class type with an implicit method.
If a class does not explicitly define a default method, the implicit default method or action is to get the instance reference of the class.
Labels: default method, Mynx compiler, Mynx compiler design, mynx language design, mynx semantic


<< Home