[erlang-questions] Element Typing in Record Declarations?

Michael Radford mrad-direct-erlang@REDACTED
Tue Feb 24 20:07:35 CET 2009

You can exploit the fact that record default values seem to be evaluated
like macros (which I personally just discovered):

-module (x).
-export ([ f/0, g/0 ]).
-record (r, { a = erlang:error({ undefined, a }), b = default_b }).
f() -> #r{}.
g() -> #r{ a = foo }.

At runtime, this works just as you'd want:

1> x:f().
** exited: {{undefined,a},
             {shell,eval_loop,3}]} **

=ERROR REPORT==== 24-Feb-2009::11:04:24 ===
Error in process <0.31.0> with exit value:

2> x:g().


Magnus Henoch writes:
> "David Mercer" <dmercer@REDACTED> writes:
> > It appears that Erlang supports type declarations in record declarations,
> Speaking of which, I sometimes find it annoying that a field without a
> default value automatically has the atom 'undefined' as an allowed
> value.  I'd like to be able to declare a record field as being required
> but having no default value, and have Dialyzer point out all the cases
> where I create such a record without specifying a value for that field.
> I guess I'm still too much of an $OTHER_LANGUAGE programmer; am I
> missing something?
> -- 
> Magnus Henoch, magnus@REDACTED
> Erlang Training and Consulting
> http://www.erlang-consulting.com/
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions

More information about the erlang-questions mailing list