[erlang-questions] next(Erlang): "Fix up records so that records are first-class citizens and not just syntax saccharine for tuples?"

Kostis Sagonas <>
Thu Nov 20 08:50:02 CET 2008


Richard O'Keefe wrote:
> 
> They could still be quite fast.  Until they've been implemented,
> we shan't know for sure.  One aspect of this is that one tends
> to write slightly different code with frames than with records.
> With records, it's quite common to see
> 
> 	f(..., R, ...) ->
> 	    ...R#foo.bar...
> 	    ...R#foo.ugh...
> 	    ...R#foo.zoo...
> 
> With frames, you write
> 
> 	f(..., R = <{bar ~ X, ugh ~ Y, zoo ~ Z}>, ...) ->
> 	    ...X...
> 	    ...Y...
> 	    ...Z...
> 
> or	f(..., R = <foo{bar ~ X, ugh ~ Y, zoo ~ Z}>, ...) ->
> 
> if you want the extra checking.  You _could_ do this with records,
> but it's less common.  And the combined form may well be faster than
> several separate field accesses.

Unrelated to the topic of records vs. structs vs. frames, but very much 
related to the above:

For quite some time now, we have been working on a tool (called 'tidier' 
-- in the spirit of 'dialyzer' and 'typer', I guess) that automatically 
cleans up Erlang code from various bad code smells and simplifies code 
fragments.  One of the transformations it does is exactly the one above 
and a bit more: it completely eliminates the use of is_record/[2,3] and 
moves all field accesses to the point where the record is matched.  For 
example, it transforms the following code:

	f(..., R, ...) when is_record(R, foo) ->
  	    ...R#foo.bar...
  	    ...R#foo.ugh...
  	    ...R#foo.zoo...

to:

  	f(..., R = #foo{bar = X, ugh = Y, zoo = Z}, ...) ->
  	    ...X...
  	    ...Y...
  	    ...Z...

(and also eliminates the R variable if it is nowhere else used).
In fact, that's one the simplest transformations that the tool performs. 
  Stay tuned!

Kostis



More information about the erlang-questions mailing list