[erlang-questions] programming tip: a note on encapsulation

Joe Armstrong erlang@REDACTED
Thu Nov 19 08:59:38 CET 2009


I hadn't thought of it that way. This reminds me of Javascript.

I define an object constructor

make_my_thing(Data) ->
    fun(data) -> Data;
       (f1)   -> fun(X)    -> do_something(X, Data)         end;
       (f2)   -> fun(X, Y) -> do_something_else(X, Y, Data) end
    end.

Then I can say

   F = make_my_thing(...)
   F(f1)(a, b)
   F(data) ...

I could make mutable objects with:

make_another_object(Data) ->
         Pid = spawn(fun() -> .... end),
         F = fun({call,X}) -> rpc(pid, X);
                   (...) -> end.


On Wed, Nov 18, 2009 at 11:08 PM, Richard O'Keefe <ok@REDACTED> wrote:
>
> On Nov 18, 2009, at 9:56 PM, Joe Armstrong wrote:
>
>> Here's a little programming technique I discovered yesterday, which I
>> rather like.
>
> It looks a whole lot like the way people used to do "objects" in T.
>   (define (make-my-object ....)
>     (lambda (Selector)
>       (case Selector
>         (field ....)
>         (method (lambda (....) ....))
>         ....))))
> with the equivalent of
>    foo = new MyObject(...);
>    foo.method(x, y)
>    foo.field
> being
>    (let ((Foo (make-my-object ....)))
>      ((Foo 'method) X Y)
>      (Foo 'field)
>
> It looks as though "Joe hates OO" may have to be rewritten!
>
>
>
>


More information about the erlang-questions mailing list