[erlang-questions] Element Typing in Record Declarations?

Zvi <>
Tue Feb 24 22:08:32 CET 2009


even better (didn't tested it yet):

-define(REQUIRED(Field), Field = erlang:error({undefined, Field})).

-record (r, { ?REQUIRED(a), b = default_b }). 

Zvi


Michael Radford-3 wrote:
> 
> 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},
>             [{x,f,0},
>              {erl_eval,do_apply,5},
>              {shell,exprs,6},
>              {shell,eval_loop,3}]} **
> 
> =ERROR REPORT==== 24-Feb-2009::11:04:24 ===
> Error in process <0.31.0> with exit value:
> {{undefined,a},[{x,f,0},{erl_eval,do_apply,5},{shell,exprs,6},{shell,eval_loop,3}]}
> 
> 2> x:g().
> {r,foo,default_b}
> 
> 
> Mike
> 
> Magnus Henoch writes:
>> "David Mercer" <> 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, 
>> Erlang Training and Consulting
>> http://www.erlang-consulting.com/
>> 
>> _______________________________________________
>> erlang-questions mailing list
>> 
>> http://www.erlang.org/mailman/listinfo/erlang-questions
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
> 
> 

-- 
View this message in context: http://www.nabble.com/Element-Typing-in-Record-Declarations--tp22183665p22190717.html
Sent from the Erlang Questions mailing list archive at Nabble.com.




More information about the erlang-questions mailing list