[erlang-patches] Re: Allow limited pattern matching on magic binaries

Evax Software <>
Thu Apr 7 11:53:06 CEST 2011

Ok, this patch has just been rejected.

Maybe I was unclear as to the goal of the patch, I'll try to explain it
a bit better.

NIF resources are implemented as magic binaries.
Magic binaries are special binaries holding private data but presenting
an apparent length of zero.

I understand that this is a good thing as the private data is not
exposed directly to the Erlang side, but this has a special side effect
for NIF resource users: any resource will match any other resource (even
from a different resource type, as well of course as the empty binary.

This is somehow dangerous as it prevents us from catching some errors
from the Erlang side, where for example assigning a resource to a
variable already bound to a resource will NOT cause an error.

Moreover not being able to distinguish NIF resources from Erlang causes
some extra work. Imagine a NIF project implementing some kind of
socket-like mechanism (erlzmq2 or libev-erlang for example), and imagine
a process handling several of these sockets in a receive loop (as in
active mode), then one must wrap resources in a tuple with a ref to be
able to match on them.

The patch implements special cases for magic binaries in the eq and cmp
operations, causing the private data to be compared, and magic binaries
with identical private data to match.

So, and maybe the patch name was badly chosen, there is no real
sub-binary pattern matching happening on private data here, it's just a
simple and straightforward way to tell resources apart, still without
exposing the private data to the Erlang side.


Le jeudi 17 mars 2011 à 11:59 +0100, Henrik Nord a écrit :
> On 03/16/2011 09:41 PM, Evax Software wrote:
> > Hi,
> >
> > The proposed patch allows limited pattern matching on magic binaries.
> > It tries to be the as non-intrusive as possible.
> > The goal of the patch is to allow matching on NIF resources, but it
> > shouldn't cause problems to other magic binary beam users.
> >
> > git fetch :evax/otp.git pattern_match_on_magic_binaries
> >
> > Evax
> >
> >
> >
> > ________________________________________________________________
> > erlang-patches (at) erlang.org mailing list.
> > See http://www.erlang.org/faq.html
> > To unsubscribe; mailto:
> >    
> Hello
> I have included this in 'pu' and will let it cook for a while.
> /Henrik Nord, Erlang/OTP
> ________________________________________________________________
> erlang-patches (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:

More information about the erlang-patches mailing list