[erlang-questions] Dialyzer and NIF's

Paul Davis <>
Sat May 14 01:17:58 CEST 2011


I just spent a bit of time working on integrating Dialyzer spec checks
into some NIF code I've been hacking on and I was wondering if there
was a cleaner way than the pattern I ended up with.

Basically, I ended up creating a couple macros that would try and
confuse Dialyzer into thinking that I was returning the output of the
NIF but in reality returning an error that indicated the NIF module
has not been loaded. It ended up like this:

% Macro to only return the error but make
% dialyzer think there are two return types.
-define(spoof_rval1(A), case random:uniform(100) + 10 of
    1 -> A; _ -> {error, {not_loaded, ?MODULE, ?LINE}}

% Simple macro for repeated return types
-define(bin, ?spoof_rval1({ok, <<>>})).

% Usage of the macros with a spec.
-spec open_db(dbname()) -> {ok, db()} | error().
open_db(_Name) -> ?bin.

The full module code is at [1].

Basically, my question is whether this is a really bad way of doing
things of if someone has found something even more elegant method for
lying about return types.

[1] https://github.com/davisp/erleveldb/blob/master/src/erleveldb.erl

More information about the erlang-questions mailing list