Thursday, January 25, 2007

Finding Closure - Closures Implicitly in Mynx

In a previous post, I discussed creating closures or features of a programming language in Mynx. The approach involved creating another class type a functor or functiod class, with a new keyword functor for the new class type.

Closures are from the functional paradigm, so emphasis on the method or function which is the primary unit. In the object-oriented paradigm, the emphasis is on a class as the primary unit. The Java programming language has anonymous inner classes,so a closure-like feature but oriented around class, not method or function.

A closure-like feature is possible in Mynx, using:

  1. A class with a method, a default constructor, and any attributes.
  2. Specification of the default method in the class.

Interesting in that instead of creating a functor or functiod class -- an explicit closure, one can use features of a Mynx class to create an implicit closure.

Like C++, which allows “()” to be overloaded, creating the potential for a closure like feature.

Example Mynx class implementing a closure:

class Closure is

    public construct is to null;

    private Int count to 0;

    public Int incInvoke(in Int i) is
        count += i;
        return count;
    end incInvoke;

    public default is to incInvoke;

end class;

Example Mynx program using an implicit closure:

program useClosure is

    Closure func to default;
    Int i to 0;

    //explicitly invoke function-method
    i = func.incInvoke(0);

    //implicitly invoke function-method as default method
    i = func(0); //=> func.incInvoke(0);

end program;


Closures are an interesting and useful feature of the functional paradigm, but mapping from the functional to the object-oriented paradigm is not a one-to-one mapping.

Mynx, akin to C++, does not have explicit support for a closure, but does have support for an implicit closure. The features of Mynx allow for a closure feature. It is intriguing that Mynx as designed does not support closures, but can indirectly -- more power from the feature set of the Mynx language, beyond the original design. A programming language that is more than the sum of its features or parts is more useful beyond the original intentions and goals.

Labels: , ,

Saturday, January 20, 2007

Any or Only Methods -- What are class methods made of?

In Mynx, issues of types and casts arise, particularly now as I implement the semantic checks. The basic problem or question is over ease of use versus strong or strict typing. The difficulty becomes more complex when considering that operator overloading is essentially associating an operator with a unary parameter method.

Any or only methods specify an instance or class method - whether the method may be overloaded (multiple methods of different parameters with the same name), or overridden (re-define the method in a sub-class derived from a super-class).

An only method can only (no pun intended...) be overridden, not overloaded. This property can be useful for creating methods of a specific purpose or function.

Example illustrating only method:


virtual class VObject is

    public only default Bool isEqual(in VObject);

end class;


The method "isEqual" can be exclusively overridden, but not overloaded. An example of overloading an only method is:-


class badMethod as VObject is

//COMPILER ERROR - only overloaded, not override doh!
     public default Bool isEqual(in Object obj) is
         //...method definition...
     end isEqual;

end class;


An example of correctly overrriding an only method is:-


class goodMethod as VObject is

//COMPILER VALID - method is overridden, not overloaded.
     public default Bool isEqual(in VObject vobj) is
         //...method definition...
     end isEqual;

end class;

In the examples, a method for class equality relation is exclusively the kind of method for that purpose. The isEqual method requires method overriding and forbidding overloading.

An only method is advantageous in that it simplifies a class considerably. In the context of the "binary method problem" of determining type, there is but a specific method in the class hierarchy.

Any is the converse and the implicit default behavior for a class method -- the method can be overriden or overloaded.

The fragment of Extended Backus-Naur syntax is:

    ACCESS ['any' | 'only'][mode][type] IDENTIFIER...

The any or only keyword specifiers specify as only one or many (1:N) -- method cardinality (to borrow a term from set theory).

Specific examples of methods:

public virtual myVClass is
    
     public only String toString;
     public any Bool isEqual(in VObject);
     public Int doHash(in myVClass);
    
end class;

The 'any' keyword allows to declare explicitly what is implicit (an important design principle of Mynx).

This feature is not present in Mynx, but in the process of implementing the semantic checks, and looking ahead to high-level language code synthesis, this feature appears to have a usefulness not found in the "Big Three" languages of C++, Java, and C#.

Labels: , , ,

Website Spy Software