[erlang-questions] Temporarily violating record type constraints annoys dialyzer

zxq9@REDACTED zxq9@REDACTED
Mon Nov 12 12:35:42 CET 2018


On 2018年11月12日月曜日 10時58分36秒 JST Roger Lipscombe wrote:
> What can I do to deal with this? Either re-structuring my code or
> persuading dialyzer that it's OK would both be acceptable.

Providing a more complete typespec can work:

  -record(widget
      {id   :: undefined | binary(),
       name :: undefined | binary(),
       size :: undefined | integer()}.

Or providing typed defaults that you know are bottom types:

  -record(widget
      {id   = <<>> :: binary(),
       name = <<>> :: binary(),
       size = 0    :: integer()}.


Depending on the rest of the code, sometimes a type definition to mask
the record is an easy way to sidestep a few wierd Dialyzer corner cases:

  -type widget() :: #widget{}.

And sometimes cutting straight through with line-by-line code conceals
the problem more directly (which allows you to ignore extra args at a
negligble performance cost:

  parse_widget(Props) ->
	  #widget{id   = proplists:get_value(id,   Props),
				name = proplists:get_value(name, Props),
				size = proplists:get_value(size, Props)}.


-Craig



More information about the erlang-questions mailing list