Using the Erlang concurrency-oriented style for object-oriented programming lacks the elegance of languages like Smalltalk. One way to solve this problem would be a pre-processor with an own syntax generating the Erlang code. But I don't like this solution because it would feel like a foreign substance. Additionally this language would have to be complete, documented, and able to use the Erlang libs. So a different idea would be a simple library, almost like gen_server, but more with OO ideas in mind. It would rely on callback modules together with the dynamic invocation of functions. First a call of
ObjRef = eos:new(my_module) or ObjRef = eos:new(my_module, Args) would create an instance as a new linked process. The initialization could use my_module:new(Args, InitialState). The initial state would be the result of a recursive initialization through calling my_module:parent_module() and initializing those modules. This behaviour, calling parent_module/0 until it returns undefined or doesn't exist in a module, would the basis for inheritence.To simplify life the EOS should only support synchronous method invocations and ignore all other Erlang messages. The call of
Result = eos:invoke(ObjRef, my_method, Args) would lead to the call of my_module:my_method(Args, State) and has to be answered with Result or {Result, NewState}. The function invoke/3 would look for the function inside the module and, if it isn't exported in that module, recursively in the parent modules. If it can't be found it would try to invoke does_not_understand/2 the same way. If even this function can't be found the system should raise an error.The dispose of the object could be done manually through
eos:dispose(ObjRef) or automatically using the typical Erlang mechanism of linked processes and their notification. Alltogether this system is really simple and it definitely doesn't compete with the standard OO languages. But it may help some experienced OO developers to feel more homelike in the Erlang world. What do you say?
0 Comments:
Post a Comment