fenollp/kju — pull request #29 by fenollp (2014-05-31 18:37:55+00:00)
Title: Key value stores (KVSs) -- maps, records and more

Finally! managed to find an accommodating way of expressing records & maps!
**ie.** *one that does not involve ambiguities in `seqExprs`*.

### The way here
The issue was that in Erlang a KVS is kind of attracting its left hand side. While there is nothing wrong with this syntax, generating the AST one would expect once you remove the expression separators is not possible.
For instance
```erlang
f () ->
Y = A,
#{k => v}.
g () ->
C = B,
#{name = "value"}.
```
once `,` removed (as is the goal of this project) you get
```erlang
f () ->
Y = A
#{k => v}
g () ->
C = B
#{name = "value"}
```
And if that's not clear enough, that would mean it would get interpreted as
```erlang
f () -> Y = A#{k => v}
g () -> C = B#{name = "value"}
```
Which is not what the Erlang code said.

### But enough chatter, here is what records & maps look like
```erlang
name of {first :: string(), last :: string()} # Also creates type 'name{}'
person of {name::name(), phone::‹pos_integer›}

have-a-look-at-the-snippets! () -> #% at examples/snippets.kju
#% Records
R = {person> name={name> first="Robert",last="Virding"}, phone=phone()}
First = {{R person> name} name> first}
#% Maps
M = {name=> {first=>"Robert",last=>"Virding"}, phone=>120+3}
{M> name := Name} {Name> first := First} #
First = {{M> name}> first} #% Same thing, but uses is_map.
#% Proplists (or Dicts ?)
P = {name= {first="Robert",last="Virding"}, phone=120+3}
First = {{P> name}> first}
```

## Discussion
Of course for any changes (improvements?) brought forth by this project to make it back to Erlang there is a discussion to have.

* First, this `>` used as a separator for updating or reading kvs is unfortunate… (another idea?)
* Proplists? yeah, specific syntax is probably not needed, but it would be for dicts for example.
* Note: if you drop proplists-syntax (`kv` in the grammar), you can get `{{M> name}> first}` as a way of reading maps.
* A space to separate a `var` from a record's name? weird, but easy to read and does not introduce a new token.
* Keep in mind that this KVS syntax does not introduce conflicts with `tuple`s or the issue described above while being *very* permissive. See [this](https://github.com/fenollp/kju/blob/0848cdf532f99d95b0eabbbc314914ee8a73e871/grammar/Kju.g4#L217-L221)
* Records & maps neutrals do look weird however: `{=}` `{=>}`…

Of course taste comes into play, and I would love to hear any criticism as to how unErlangish this syntax may be!