<div>
Hi All,
</div><div><br></div><div>I have a question on the behaviour of Dialyzer with higher-order functions. Consider the following trivial code:</div><div><br></div><div><div><font face="'Courier New'" style="font-size: 12px;">-module(test).</font></div><div><font face="'Courier New'" style="font-size: 12px;"><br></font></div><div><font face="'Courier New'" style="font-size: 12px;">-compile(export_all).</font></div><div><font face="'Courier New'" style="font-size: 12px;"><br></font></div><div><font face="'Courier New'" style="font-size: 12px;">-record(rec1, {fred}).</font></div><div><font face="'Courier New'" style="font-size: 12px;">-record(rec2, {harry}).</font></div><div><font face="'Courier New'" style="font-size: 12px;"><br></font></div><div><font face="'Courier New'" style="font-size: 12px;">test() -></font></div><div><font face="'Courier New'" style="font-size: 12px;"> #rec2{} = do_something(1, fun map/1).</font></div><div><font face="'Courier New'" style="font-size: 12px;"><br></font></div><div><font face="'Courier New'" style="font-size: 12px;">do_something(Num, MapFun) -></font></div><div><font face="'Courier New'" style="font-size: 12px;"> MapFun(Num).</font></div><div><font face="'Courier New'" style="font-size: 12px;"><br></font></div><div><font face="'Courier New'" style="font-size: 12px;">map(_X) -></font></div><div><font face="'Courier New'" style="font-size: 12px;"> #rec1{}.</font></div><div><font face="'Courier New'"><span style="font-size: 12px;"><br></span></font></div></div>
<div><div>If I run Dialyzer against it, I get no errors or warnings logged, even though it's quite apparent that it will fail. I would have expected that Dialyzer could infer that the return type of do_something was the return type of MapFun; within the context of do_something(), that is clearly any(), but within the context of test() I would have thought that it could see that do_something() is actually going to return a #rec1{} record, that being the return type of map().</div><div><br></div><div>If I replace the call to MapFun with a call to map(), as in:</div><div><br></div><div><div><font face="'Courier New'" style="font-size: 12px; ">do_something(Num, _MapFun) -></font></div><div><font face="'Courier New'" style="font-size: 12px; "> map(Num).</font></div></div><div><font face="'Courier New'" style="font-size: 12px; "><br></font></div><div>Then everything works as expected - Dialyzer correctly tells me that my code is broken. Is there anything I can do to "teach" dialyzer how to interpret the MapFun? It works as I would like with functions such as lists:map():</div><div><br></div><div><div><font face="'Courier New'" style="font-size: 12px;">-module(test).</font></div><div><font face="'Courier New'" style="font-size: 12px;"><br></font></div><div><font face="'Courier New'" style="font-size: 12px;">-compile(export_all).</font></div><div><font face="'Courier New'" style="font-size: 12px;"><br></font></div><div><font face="'Courier New'" style="font-size: 12px;">-record(rec1, {fred}).</font></div><div><font face="'Courier New'" style="font-size: 12px;">-record(rec2, {harry}).</font></div><div><font face="'Courier New'" style="font-size: 12px;"><br></font></div><div><font face="'Courier New'" style="font-size: 12px;">test() -></font></div><div><font face="'Courier New'" style="font-size: 12px;"> [#rec2{}] = lists:map(fun map/1, [1]).</font></div><div><font face="'Courier New'" style="font-size: 12px;"><br></font></div><div><font face="'Courier New'" style="font-size: 12px;">map(_X) -></font></div><div><font face="'Courier New'" style="font-size: 12px;"> #rec1{}.</font></div></div><div><br></div><div>With the above, I get the expected warning. I've looked at lists.erl, and it doesn't look to be doing anything different. It does have some -spec directives, but adding those in my code did not appear to help.</div><div><br></div><div>Any hints?</div><div><br></div><div>Cheers,</div><div><br></div><div>Steve</div><div><br></div><div>-- </div>Steve Strong<div>@srstrong</div><div><br>Sent with <a href="http://www.sparrowmailapp.com/?sig">Sparrow</a></div><div><br></div></div>