[e-lang] record syntax for E, please help
Kevin Reid
kpreid at mac.com
Tue Jan 8 18:52:13 EST 2008
On Jan 7, 2008, at 19:56, Pierre Radermecker wrote:
> Does E provide syntactic facilities for "records" ? I like to do
> something similar to the Erlang record syntax :
>
> -record(language,{name = 'E', popularity= high, rating}).
>
> Declaring a constructor for each default/optional data does not sound
> like a practical option ;-) Is it the default way to create data
> container ?
>
> I would like to have an immutable data store that can be built quickly
> according to the data at hand together with an explicit "template"
> that
> indicate which kind of data the objects can hold. So "dictionary" or
> "map" seems like a poor option.
>
> Of course if I can do "pattern matching" (or whatever) to quickly
> create, read or update the container that would be wonderful.
Well, let's sketch something out. I've been thinking about a library
to provide Haskell-style algebraic data types, which would need much
of the same mechanism as this problem.
How would you like this?
? def [=> makeLanguage] := recordType`name :$String := ${"E"},
> popularity := $high,
> rating :$int`
(the := is used to match E's map-default syntax, which might change)
? def l := makeLanguage([name => "Java",
> rating => 64])
# value: <language name => "Java", popularity => <high>, rating => 64>
? l.name()
# value: "Java"
? pragma.enable("call-pattern")
> def makeLanguage([=> name, => rating] | _) := l
> rating
# value: 64
Now, this might be a bit inefficient in current implementations
(though maps in arguments really ought to be optimized, considering);
one could instead pack it in the verb:
? def l := makeLanguage."name,rating"("Java", 64)
# value: <language name => "Java", popularity => <high>, rating => 64>
? def makeLanguage."popularity,rating"(p, r) := l
> p
# value: <high>
What I'm intending to show is, there are ways to do this without
extending the fundamental syntax. We just need to pick a good one.
What do you think? What would you prefer?
--
Kevin Reid <http://homepage.mac.com/kpreid/>
More information about the e-lang
mailing list