[erlang-questions] Dialyzer "no local return" warning for anonymous functions

Kostis Sagonas kostis@REDACTED
Wed Dec 15 10:08:52 CET 2010


Bernard Duggan wrote:
> Hi all,
> 
> We have a bit of code that looks something like this:
> 
> validate(A, B) ->
>     try
>         mnesia:transaction(fun() ->
>             some_test_db_operations(A, B),
>             mnesia:abort(all_valid)
>         end),
>     catch
>         exit:{aborted, all_valid} -> ok;
>         exit:{aborted, Error} -> {error, Error}
>     end.
> 
> The purpose is to "try out" some mnesia operations on un-validated data 
> to ensure that they'll all work.  It's kind of a neat trick (though no 
> doubt falling fairly far from "best practise" in terms of use of 
> exceptions), but it annoys dialyzer in a way that I can't figure out how 
> to avoid.
> 
> Dialyzer (as of R14B01, at least) quite correctly reports that the 
> anonymous function in the transaction has no local return.  For normal 
> functions, I'd work around this by simply giving it a -spec with 
> no_return() as the return type, and everyone is happy.  As far as I 
> know, though, there's no way to -spec an anonymous function, nor is 
> there any obvious way to make that particular function not anonymous.

It's too early in the morning for me, but doesn't the following do it?
(Warning: untested -- I also think the parentheses aroung the fun object 
are not really needed, but perhaps the code is clearer with them)

   validate(A, B) ->
       try
           mnesia:transaction((fun my_funky_mnesia_test/2)(A,B))
       catch
           exit:{aborted, all_valid} -> ok;
           exit:{aborted, Error} -> {error, Error}
       end.

   -spec my_funky_mnesia_test(some_t(), another_t()) -> no_return().

   my_funky_mnesia_test(A, B) ->
       some_test_db_operations(A, B),
       mnesia:abort(all_valid).

Cheers,
Kostis


More information about the erlang-questions mailing list