[erlang-questions] how: Matching empty binaries at the beginning

Juan Jose Comellas <>
Thu Nov 15 20:35:24 CET 2007


I don't think that is the cause, because it doesn't work for non-empty
binaries either. Look at this:

> ABC = <<"ABC">>.
   <<"ABC">>

> A = <<"A">>.
   <<"A">>

> <<A/binary, BC/binary>> = ABC.
   ** exited: {{badmatch,<<"ABC">>},[{erl_eval,expr,3}]} **

And if I add the size of the binary it now works:

> <<A:1/binary, BC/binary>> = ABC.
   <<"ABC">>


On 11/15/07, Jeremy Wall <> wrote:
>
>
>
> On 11/15/07, Juan Jose Comellas <> wrote:
> >
> > Why is this expression not matching?
> >
> > A = <<"ABC">>,
> > Empty = <<>>,
> > <<Empty/binary, Tail/binary>> = A.
>
> I was expecting that this would result in 'Tail' being bound to <<"ABC">>,
> > but instead I'm getting a badmatch error. However, if I specify the length
> > of the 'Empty' variable, it works:
> >
> > <<Empty:0/binary, Tail/binary>> = A.
>
> And if I put 'Empty' as the last part of the pattern it also works:
>
>
>
> I suspect this is because empty binaries don't have a size so erlang can't
> determine the size for you meaning you have to explicitly specify a size of
> 0 for erlang to figure out how to match it. Might be nice if it could figure
> out the 0 part itself but not neccessarily a bug. Maybe a Core developer
> could shed more light?
>
> <<Head/binary, Empty/binary>> = A.
>
>
>
> Since /binary on the end just means the rest of the binary I would expect
> this to work exactly as it does without specifying a size.
>
> Is matching for binaries supposed to work like this or is it a bug?
> >
> >
> > _______________________________________________
> > erlang-questions mailing list
> > 
> > http://www.erlang.org/mailman/listinfo/erlang-questions
> >
>
>
>
> --
> Jeremy Wall
> http://jeremy.marzhillstudios.com
> 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20071115/c56926bf/attachment.html>


More information about the erlang-questions mailing list