[erlang-questions] If you are homesick for object.selector

ok@REDACTED ok@REDACTED
Sat Jan 26 03:41:54 CET 2013


David Goehrig wrote:
>
>   of(Object,Property) -> proplists:get_value(Property,Object).
...
>
>     of(of(of(Character,weapon),ability),cost).

At this point it might be useful to mention R (or rather the S
language that R implements).  Leaving S4 objects to one side as
a distraction, S "objects" are immutable.  The rule in S is that

    f(e1, e2) <- e0

is syntactic sugar for

    e1 <- 'f<-'(e1, e2, e0)

with the rule applied recursively (although I've never seen the
details made clear).  Even

    a[i] <- x

is just shorthand for

    a <- '[<-'(a, i, x).

Imagine Erlang to be extended with a := operator with
the following semantics:

    var := expr

        if var is not bound in the current path, var = expr
        if var is bound, introduce a new variable var', var' = expr,
        and use var' instead of var downstream
        At if/case/receive/... termini, reconcile live variables
        as with a SSA phi-function

    f(E1,...,En) := E0

        rewrite as E1 := 'f:='(E1, ..., En, En)

    const := expr

        illegal

This would in no way affect the underlying 'no mutable data'/
single assignment nature of Erlang.  Indeed, the VM would not
be altered in even the slightest way.  It's all compile time.
Then you could write

     of(of(of(Character,weapon),ability),cost) := 123

This requires a function 'of:='(Thing, Slot, New_Value).

You could also define

    cost(X) -> of(X, cost).
    'cost:='(X, C) -> 'of:='(X, cost, C).
    ...

      cost(ability(weapon(Character)) := 123

It's all just pure values and functions underneath.
Would _this_ satisfy the complaints?






More information about the erlang-questions mailing list