Functor Functional Features from C++ in Mynx
One feature I’ve considered adding from C++ to Mynx (as I implement the semantic checks, right now deep into expression statement sentence semantics) is a functional feature, in the functional programming paradigm it is a closure, but in C++ it is called a functor -- a class callable like a method.
In C++, a functor is created by overloading the method call operator or the parenthesis. So it is by overloading that a functor can be created...it is an implicit feature, not explicit.
Code for a functor in C++ class is:
Code to use a C++ functor is:
The call to
A functor is a very interesting feature, but one fraught with peril; a functor can create the infamous Abstraction Inversion antipattern. And as the feature is achieved implicitly, it is possible to implicitly and unintentionally create.
In Mynx, class as a function would be an interesting feature to have -- a program can be considered similar a callable class, just with the restrictions of non-class use imposed, and callable only externally. So a functor would be in the same conceptual framework.
Only for Mynx, it would be explicit in use and intent to create. Code for a functor is at the same conceptual level as a program or class. The syntax for a functor is similar to a class or program unit:
The primary difference from the C++ functor is that the Mynx does not require a nor use a class constructor, the functor is useable as it is...the code for using a functor without creating an instance:
The main question is if a functor should simply be a method like a static or stateless method, or if a functor should be created with specific parameters. The same program with a stateful or created functor:
The stateful functor carries information from its creation, so that a functor can use different parameters to achieve different behaviors, instance impacts the behavior of an instance of a functor.
The stateless functor is less like a functional closure and more like an anonymous static method of a class. The stateful functor has the advantage that if no parameters are desired, it can be stateless. For the stateless functor, it is very difficult except to pass in some object instance that is used (a delegate) which obviates the whole point of a functor.
The syntax for a stateful functor is similar to a stateless, the instance creation is defined inside a clause that begins with the keyword as:
The second parameter list is similar to the parameter list for a constructor in a class, only in this case for a functor.
An important distinction between a Mynx functor and the C++ is that the Mynx functor is intentionally explicit, and so avoids implicit features to achieve the functor. It is not as easy to accidentally or implicitly use an abstraction inversion as it is with C++. The functor is intentionally explicit, not implict through an overloaded operator in the class unit.
Sometime in the future, I might add this construct to the Mynx language. I don't want to start re-designing the language as I implement the semantic checks phase--but the feature would be nice, or would it? That's something I'm going to consider. But for now, to use a building under construction analogy, I do not want to change the building lobby or courtyard while the construction is ongoing.
In C++, a functor is created by overloading the method call operator or the parenthesis. So it is by overloading that a functor can be created...it is an implicit feature, not explicit.
Code for a functor in C++ class is:
#include <iostream>
using namespace std;
class Comparator_Functor
{
public:
Comparator_Functor(int i) : Number(i) { }
bool operator()(int other_int) { return other_int == Number; }
private:
bool isEQ(int val){ return val == Number; }
int Number;
};
Code to use a C++ functor is:
int main(int argc, char **argv)
{
Comparator_Functor isZero(1);
if (isZero(argc))
cout << "argument count is zero";
else
cout << "argument count is not zero";
cout << endl;
return 0;
}//end main
The call to
isZero is calling the class Comparator_Functor as a method - a functor. What is interesting is C++ does not explicitly support this feature - it is achieved through its features of operator overloading, implicit feature of the language.A functor is a very interesting feature, but one fraught with peril; a functor can create the infamous Abstraction Inversion antipattern. And as the feature is achieved implicitly, it is possible to implicitly and unintentionally create.
In Mynx, class as a function would be an interesting feature to have -- a program can be considered similar a callable class, just with the restrictions of non-class use imposed, and callable only externally. So a functor would be in the same conceptual framework.
Only for Mynx, it would be explicit in use and intent to create. Code for a functor is at the same conceptual level as a program or class. The syntax for a functor is similar to a class or program unit:
functor Bool aFunctor(in Int i) is
return i > 0;
end functor;
The primary difference from the C++ functor is that the Mynx does not require a nor use a class constructor, the functor is useable as it is...the code for using a functor without creating an instance:
program useFunctorStateless is
if(aFunctor(3)
IO.putln("Greater than zero");
else
IO.putln("Lesser or equal to zero");
end if;
end program;
The main question is if a functor should simply be a method like a static or stateless method, or if a functor should be created with specific parameters. The same program with a stateful or created functor:
program useFunctorStateless is
aFunctor af to 0; //create instance of functor unlike stateless
if(af(3)
IO.putln("Greater than zero");
else
IO.putln("Lesser or equal to zero");
end if;
end program;
The stateful functor carries information from its creation, so that a functor can use different parameters to achieve different behaviors, instance impacts the behavior of an instance of a functor.
The stateless functor is less like a functional closure and more like an anonymous static method of a class. The stateful functor has the advantage that if no parameters are desired, it can be stateless. For the stateless functor, it is very difficult except to pass in some object instance that is used (a delegate) which obviates the whole point of a functor.
The syntax for a stateful functor is similar to a stateless, the instance creation is defined inside a clause that begins with the keyword as:
functor Bool aFunctor(in Int i) as (in Int j) is
return i > j;
end functor;
The second parameter list is similar to the parameter list for a constructor in a class, only in this case for a functor.
An important distinction between a Mynx functor and the C++ is that the Mynx functor is intentionally explicit, and so avoids implicit features to achieve the functor. It is not as easy to accidentally or implicitly use an abstraction inversion as it is with C++. The functor is intentionally explicit, not implict through an overloaded operator in the class unit.
Sometime in the future, I might add this construct to the Mynx language. I don't want to start re-designing the language as I implement the semantic checks phase--but the feature would be nice, or would it? That's something I'm going to consider. But for now, to use a building under construction analogy, I do not want to change the building lobby or courtyard while the construction is ongoing.
Labels: functional construct, functional features, functor, functor class

