Structs (was RE: Record selectors)

Chris Pressey <>
Wed Jan 15 02:31:45 CET 2003


On Tue, 14 Jan 2003 10:41:44 +0100 (CET)
Joe Armstrong <> wrote:

> [...]
>   With structs you can write things like:
> 
> 	A = ~{name="joe", footSize=42},   %% define a struct
> 	A.footSize,                       %% access a field
> 	B = ~A{likes="motorbikes"},       %% add a new field
> 	~{likes=X, name=N} = B}           %% pattern match etc.
> 
>   *without* any record defs.

Hm, OK, I've read the paper, let me see if I've got this straight.

Structs are basically dictionaries a la the dict module, except:

1) you can pattern-match on their contents
2) they have their own special syntactic sugar
3) they're implemented directly in the VM for performance

Well, #1 is a great advantage over dicts, of course.

#2 is a mixed blessing - A.footSize is easier to read and to type out than
dict:fetch(A, footSize), but it's also rather unorthogonal, symbolspace
is getting more and more crowded, "~" is easy to confuse with "-" in many
fonts, plus I personally think tildes are kind of ugly :)

#3 is arbitrary (there's no reason dicts or any other data type couldn't
be implemented in the VM too.)

Also, since it's one of the considerations that sparked this thread, it's
worth noting that structs are in no way safer that records; in fact,
they're so flexible that they're arguably less safe.  Records have
rigid structure, while structs, like dicts, are just random bags of data.

So, I don't want to sound too negative, but honestly, I'm less than
thrilled by the idea.

But, that's possibly because the world I live and program in puts an
emphasis on validation.  I still think you can get more "bang for your
buck" with "objects" - if you write a module for each data type you use,
you have full control over the interface, you can screen out bad data
before it gets into the aggregate, you can use whatever storage scheme you
like (and change it at a later date,) you have a convenient place to group
all the applicable functions, etc etc, at a base cost surely not *too*
much greater than using structs...

Forgive me, but is Erlang really so object-shy that encapsulation is
something that we feel we can afford to avoid simply because "they" have
turned it into a meaningless buzzword?

-Chris



More information about the erlang-questions mailing list