<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Hi Roger,<div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>According to how you <i class="">use</i> your record, its spec should actually be…</div><div class=""><br class=""></div><div class="">-record widget {<br class="">   id :: undefined | binary(),<br class="">   name :: undefined | binary(),</div><div class="">   size :: undefined | integer()</div><div class="">}.</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">       </span>That will <i class="">silence</i> dialyzer or, putting it in the right perspective: That will be a spec that matches how your code actually treats that record.</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>And that’s because: <i class="">What happens if </i>Props<i class=""> </i><b class=""><i class="">doesn’t</i></b><i class=""> have a tuple for </i>name<i class="">?</i></div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space: pre;">       </span>If not having a {name, Name} tuple in Props is an invalid scenario, you should raise some sort of error.</div><div class=""><span class="Apple-tab-span" style="white-space:pre">  </span>In that case I would recommend you to keep the record definition as-is, but fill the whole record at once:</div><div class=""><br class=""></div><div class=""><div class="">parse_widget(Props) -></div><div class="">  #widget{</div><div class="">    id = get_value(id, Props),</div><div class="">    name = get_value(name, Props),</div></div><div class="">    …</div><div class="">  }.</div><div class=""><br class=""></div><div class="">get_value(Key, Props) -></div><div class="">  {Key, Value} = lists:keyfind(Key, 1, Props),</div><div class="">  Value.</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">      </span>That way, get_value/2 will raise an error if a property is missing.</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>If not having a {name, Name} tuple in Props is a valid scenario but you still don’t want that record property to be undefined, you will need a sane default for it.</div><div class=""><span class="Apple-tab-span" style="white-space:pre">     </span>In that case, you should amend your record definition as…</div><div class=""><br class=""></div><div class=""><div class="">-record widget {<br class="">   id = <<>> :: binary(),<br class="">   name = <<>> :: binary(),</div><div class="">   size = 0 :: integer()</div><div class="">}.</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">       </span>Hope this helps :)</div><div class=""><br class=""></div><div class=""><div style="text-align: start; text-indent: 0px;" class=""><div class=""><span id="docs-internal-guid-e691a4cc-056a-0210-b8b7-ea8d87d888ad" class=""><span style="font-size: 11pt; font-family: Arial; font-weight: 700; font-variant-ligatures: normal; font-variant-position: normal; font-variant-numeric: normal; font-variant-alternates: normal; font-variant-east-asian: normal; vertical-align: baseline; white-space: pre-wrap;" class=""><hr class=""></span></span><div class=""><b class=""><i class=""><a href="http://about.me/elbrujohalcon" class="">Brujo Benavides</a></i></b></div></div></div><div style="color: rgb(0, 0, 0); font-family: 'Trebuchet MS'; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><b class=""><br class=""></b></div><br class="Apple-interchange-newline">
</div>
<div><br class=""><blockquote type="cite" class=""><div class="">On 12 Nov 2018, at 07:58, Roger Lipscombe <<a href="mailto:roger@differentpla.net" class="">roger@differentpla.net</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">I've got a record defined as follows (e.g., and very simplified):<br class=""><br class="">-record widget {<br class="">    id :: binary(),<br class="">    name :: binary(),<br class="">    size :: integer()<br class="">}.<br class=""><br class="">I parse that from (e.g.) a proplist:<br class=""><br class="">parse_widget(Props) -><br class="">    parse_widget(Props, #widget{}).<br class=""><br class="">parse_widget([{name, Name} | Rest], Acc) -><br class="">    parse_widget(Rest, Acc#widget { name = Name });<br class="">% etc.<br class=""><br class="">Dialyzer isn't happy that my fields are initially set to 'undefined',<br class="">even though this only occurs during the parsing step, and isn't a big<br class="">deal.<br class=""><br class="">What can I do to deal with this? Either re-structuring my code or<br class="">persuading dialyzer that it's OK would both be acceptable.<br class="">_______________________________________________<br class="">erlang-questions mailing list<br class=""><a href="mailto:erlang-questions@erlang.org" class="">erlang-questions@erlang.org</a><br class="">http://erlang.org/mailman/listinfo/erlang-questions<br class=""></div></div></blockquote></div><br class=""></div></body></html>