Reflection in Erlang.

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Reflection in Erlang.

Fredrik Linder-2
> > Using this design you could have the adaptor:getStub(...) return
> > my_behaviour_implementation and later use that to make the calls.
>
> I wan't to wrap  objects from other language, so the problem
> is how to
> dinamicly impersonate an object with an arbitrary interface
> in Erlang.  I  
> can solve this using the simple call:
>
> adaptor:callMethod(Obj, "method1", Args).
>
> but I was looking for some "Sugar Syntax". The ideal was
> something like:
>
> Obj:method1(arg1, arg2).
>
> But I known, Erlang is not an OO language :)

The easiest fit is probably what you suggest yourself:

Type:Method(Obj, Arg1, Arg2) -> {Result, Obj} | exit(Reason)
or
Type:Method(Obj, Arg1, Arg2) -> {ok, Result, Obj} | {error, Reason, Obj}

Another approach could be to use funs to wrap in the specific object:

initObj(InitArgs) ->
    fun(method1, CallArgs) -> something(InitArgs, CallArgs);
       (method2, CallArgs) -> somethingelse(InitArgs, CallArgs);
       (Method, CallArgs) -> parent:initObj(InitArgs)
    end.

Obj = initObj(InitArgs),
{Result, Obj2} = Obj(method1, [arg1, arg2]),
{Result, Obj3} = Obj2(method2, [arg]).

Play around have fun!
/Fredrik