#erlang #oop #patterns #undocumented #syntactic sugar
epoch: 1260352260
The story of module extension continues. Another still undocumented feature of recent Erlang versions is to have parametrized modules and module instances. Have a look at this code:
-module(mytest, [Var1, Var2]). -export([getInstvars/0]). getInstvars() -> [Var1,Var2].
With this you can have variables known in module scope. These variables are of course immutable. Moreover you can instantiate a module and bind a variable to that instance. Then you can use the variable as you would do with objects in an OOP language. This is a corresponding session, using the automatically exported new/2 function and a kind of anonymous instance:
1> H = mytest:new(1,2).
{mytest,1,2}
2> H:getInstvars().
[1,2]
3> {mytest,4,5}:getInstvars().
[4,5]
I’m still not sure if this is a good idea. The migration from your prefered imperative OOP language to the functional world of Erlang may become easier if you recover some of your well known structures but it may lead to misuse before you understood the benefits of functional programming well enough.
I’d rather appreciate the introduction of packages or namespaces to structure modules hierarchically in Erlang, or the nesting of modules like OCaml does. A blueprint of parametrized modules may be OCaml functors as well but I do not (yet) know enough about that language. We will see how the community will use or misuse this feature.
Powered by Tumblr; designed by Adam Lloyd and Ingo Schramm.