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: , ,

Website Spy Software