[erlang-questions] Element Typing in Record Declarations?

Michael Radford <>
Tue Feb 24 20:14:07 CET 2009


Oh yeah, I forgot to note that this gives you the dialyzer output you
want as well:

% dialyzer -c x.beam
  Checking whether the initial PLT exists and is up-to-date... yes
  Proceeding with analysis... 
x.erl:4: Function f/0 has no local return
done (warnings were emitted)

Mike

Michael Radford writes:
> 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



More information about the erlang-questions mailing list