[erlang-questions] Reference binary comparison problem

Michael Santos michael.santos@REDACTED
Fri Jun 15 17:30:05 CEST 2012


On Fri, Jun 15, 2012 at 04:36:21PM +0200, Sverker Eriksson wrote:
> Here's an earlier discussion on this topic:
> 
> http://erlang.2086793.n4.nabble.com/Allow-limited-pattern-matching-on-magic-binaries-td3382967.html
> 
> It turns out that it's not that trivial as one might first think.
> 
> 
> /Sverker, Erlang/OTP Ericsson
> 
> 
> Dmitry Groshev wrote:
> >There is a little problem with reference binaries that are used with
> >NIFs: they all are treated as equal. It's quite uncomfortable when one
> >works with NIFs. I can try to make a patch to fix this (I'm not sure
> >about it's quality though) or I can just wait for OTP team (it looks
> >like quite small fix) What's better?

In practice, it's simple to work around this in your NIF. Return a tagged
tuple with a ref, something like:

    return enif_make_tuple2(env,
        atom_ok,
        enif_make_tuple4(env,
            atom_resource,
            type,
            enif_make_ref(env), res));

Which ends up being {ok, {resource, foo, #Ref<0.0.0.32>, <<>>}} and can
be defined as a record: #resource{type = foo, ref = Ref, res = Res}



More information about the erlang-questions mailing list