[erlang-questions] Here's hoping we get frames or structs in 2009!

Gleb Peregud gleber.p@REDACTED
Mon Jan 12 14:16:36 CET 2009


On Mon, Jan 12, 2009 at 1:59 PM, Kostis Sagonas <kostis@REDACTED> wrote:
> James Hague wrote:
>> I've been using Erlang as my primary language for personal projects
>> for some years now, though I also use and enjoy Perl, Python, REBOL,
>> C++, Lua, and occasionally Forth.  Quite often I'm surprised at how
>> much easier it is write certain types of code in Erlang...and I'm also
>> surprised at how awkward it is to write other types of code in Erlang.
>>  Sometimes the awkwardness is because I just can't think of a pretty
>> way to avoid destructive updates in an algorithm that's inherently
>> destructive.  But much of the time it's from working around the lack
>> of lightweight dictionaries or hashes.
>>
>> In Perl, I don't think twice about creating hashes:
>>
>>    (width => 1280, height => 1024, colors=655536)
>>
>> The Python version is similarly clean:
>>
>>    dict(width=1280, height=1024, colors=65536)
>>
>> In Erlang, I can create a property list:
>>
>>    [{width,1280}, {height,1024}, {colors,65536}]
>>
>> which isn't bad in itself, but not being able to pattern match on
>> these is what hurts.I work around it by manually grabbing properties,
>> but the code ends up bulky and contrived.  Yeah, we've all discussed
>> this for years, so here's hoping there's some movement on this front.
>> Syntactically, the version I like the most at the moment is the record
>> syntax without a record name:
>>
>>    #{width=1280, height=1024, colors=65536}
>
> Perhaps I am missing the obvious but I fail to see how the presence of
> the record name prevents you from doing this today -- especially for
> things that have a fixed number of "properties" (e.g. width, height,
> colors) as the above.
>
> Kostis
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>


Probably James means that such structure could be used in such a way:

Options0 = #{width=1280, height=1024},
...
Options = Options0#{colors = 256},
...
adjust_screen(Options).

adjust_screen(#{colors = 65536}) -> erlang:error(unsupported);
adjust_screen(Options = #{width = W, height = H, colors = _}) -> %%
'colors = _' would match iff this key is set
    io:fwrite("selected colors ~b~n", [Options.colors]),  %% or it can
be Options#colors (confusing) or Options#.colors (awkward...)
    do_something(...).

I opt for inclusion of such structure into the language. It will
combine power of dict (dynamic set key-value pairs) and power of
tuples/records (pattern matching)

--
Gleb Peregud
http://gleber.pl/

Every minute is to be grasped.
Time waits for nobody.
-- Inscription on a Zen Gong



More information about the erlang-questions mailing list