[erlang-questions] Dialyzer and NIF's

Paul Davis paul.joseph.davis@REDACTED
Sat May 14 17:13:35 CEST 2011


Anthony,

Excellent! That's exactly what I needed.

Paul

On Sat, May 14, 2011 at 3:35 AM, Anthony Ramine <nox@REDACTED> wrote:
> You should probably use erlang:nif_error/{1,2}.
> http://www.erlang.org/doc/man/erlang.html#nif_error-1
>
> Le 14 mai 2011 à 01:17, Paul Davis a écrit :
>
>> Hiya,
>>
>> 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}}
>> end).
>>
>> % 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
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
>
> --
> Anthony Ramine
> Dev:Extend
> http://dev-extend.eu
>
>
>
>
>



More information about the erlang-questions mailing list