[erlang-questions] Erlang Syntax - again

Ulf Wiger (TN/EAB) ulf.wiger@REDACTED
Tue Mar 11 16:27:29 CET 2008


Hynek Vychodil skrev:
> 
> 
>     But what I would like to have in Erlang is definitely better
>     introspection /
>     reflection capabilities. I would like to have records as runtime
>     structures,
>     so I could write:
> 
>       Var#{x = foo, y = bar}
> 
>     where Var could be any record that has slots x and y (it might have
>     others),
>     record_info would be a normal function that can operate on variables.
> 
> 
> It is misunderstand what record is. Record is not this sort of things. 
> Use our own structure for this purpose. Record is not object, hash, 
> dictionary or whatever you really want. Record is not designed for some 
> sort of inheritance. Record is structure for memory friendly storage of 
> fixed "records" a which one will be changed very rare and just only by 
> design.

I don't think that these particular aspects of the current
record syntax are a "feature", but rather a pretty severe
drawback.

I have used a hack called exprecs (*), which I think addresses
some of the main problems. For one thing, it allows me to
"export" record accessors from a certain module, thus letting
users of the records read and modify attributes of a record
object without having to ensure that their own module was
compiled exactly the right version of some include file
containing the record definition.

I've also found this quite comfortable e.g. when writing
a Diameter stack, where a record is the most intuitive
representation of a message, but the central logic needs
to be able to check the value of a given attribute (e.g.
'Destination-Host'), even though it can't possibly know
all add-on message record formats at compile-time:

has_dest_host(Rec, Dict) ->
     try Dict:'#get-'('Destination-Host', Rec) of
         T ->
             T /= undefined andalso T /= []
     catch
         error:_ ->
             false
     end.

Not that I'm putting this forth as an example of
better syntax, but as an example of what one might want
to do.

In general, I think that records and macros introduce
compile-time dependencies that can be very painful in large
projects. Given that just about everything else can be
properly encapsulated and managed at runtime, with polymorphism
and all, records and macros stick out like a sore thumb.

BR,
Ulf W


(*) http://forum.trapexit.org/viewtopic.php?p=21790#21790



More information about the erlang-questions mailing list