erlangdialyzererlang-nif

Erlang: NIFs and dialyzer warning


When implementing NIFs, Dialyzer gives me

Function crc16/1 has no local return

probably because I do exit in the .erl module (like the official docs recommend):

-module(my_nifs).
-export([crc16/1]).

-on_load(init/0).

init() ->
    ok = erlang:load_nif("../nifs/my_nifs", 0).

-spec crc16(_Binary :: binary()) -> non_neg_integer().
crc16(_Binary) ->
    exit(nif_library_not_loaded).
...

And generally, it seems that using exit/1 always makes Dialyzer to complain with this message (-spec .. -> no_return() doesn't help).

How can this be fixed?


Solution

  • You could use erlang:nif_error/1/2 which where created just for that.