[erlang-questions] Maps

Richard A. O'Keefe ok@REDACTED
Wed May 15 03:32:57 CEST 2013

> My personal use case is probably not that universal, but I believe it also applies to anyone who has to access or modify JSON or equivalent (not XML). How do you access deep values in JSON in Erlang? How do you update a value in JSON?

Not present-day Erlang, but we could do it.

(1) Copy Pop-2.
    Define that E0.[m:]f[([E1,...,En])]
    is equivalent to [m:]f(E0[,E1,...,En]).

    This means that X.hd and hd(X) are the same expression,
    and X.lists:sublist(Y, Z) and lists:sublist(X, Y, Z) are the same expression.

    This is a trivial source->source transformation that gives functional
    semantics OO-like syntax.

    Restriction: the dot must be immediately adjacent to the m or the f.
    Similar restrictions are found in other languages.  For example, in
    Haskell, m.f is "f in module m" but m . f is "composition of functions
    m and f".  People live with it.

(2) With a slight twist.
    Define that E0.[E1]
    is equivalent to slot(E0, E1), whatever slot is in scope.

    So if you do
	-import(json, [slot/2]).
    then J.[1].[foo].[3].[oar] would be the functional
    equivalent of J[1]["foo"][3]["oar"] in Javascript.

    Again, we have function *semantics* hiding under OO-ish syntax.
    (1) and (2) can be mixed freely.

    This is also a pretty trivial source->source transformation.
(3) Define update according to the EEP that I haven't finished yet,
    so that
	J.[1].[foo].[3].[oar] := Whatever
    creates a new variable J' which is used downstream in the place
    of J and does pretty much the right thing.

    Getting all the details of this right is non-trivial, but
    definitely doable.

All of this is purely at the level of syntax.
None of it is specific to records, frames, maps, or Dho-Nha curves.

We *DON'T* have this yet, but we *COULD*,
and it would require NO changes to the VM or the compiler back-end,
so BEAM code generated with a new compiler would be backwards

More information about the erlang-questions mailing list