[erlang-questions] Construct and match binaries

Jayson Barley <>
Tue Nov 6 23:26:08 CET 2012


Switch the match around and it works.
1> A = <<$a>>.
<<"a">>
2> B = <<$b>>.
<<"b">>
3> <<"a:b">> = <<A/binary, $:, B/binary>>.
<<"a:b">>

If I understand the way it works correctly you are attempting to assign the
right side to the left side in your example. If I am wrong I am sure one of
the smarter Erlangers will flog me.

Jayson


On Tue, Nov 6, 2012 at 2:16 PM, Olav Frengstad <> wrote:

> Hey,
>
> I'm trying to wrap my head around a problem I have with constructing
> and matching a binary.
>
> Take the following example (will fail with "a binary field without
> size is only allowed at the end of a binary pattern"):
>
> 2> A = <<$a>>,  B = <<$b>>,
> 2> <<A/binary, $:, B/binary>> = <<"a:b">>.
> * 2: a binary field without size is only allowed at the end of a binary
> pattern
>
> Using size specification yields the expected result:
> 3> <<A:1/binary, $:, B:1/binary>> = <<"a:b">>.
> <<"a:b">>
>
> Even equality checks works:
> 4> <<A/binary, $:, B/binary>> == <<"a:b">>.
> true
>
> I would assume that the size of the binary is known when performing
> construction and therefore the first example should work (considering
> <<X/binary,...>> equality checks works).
>
> Can anyone shed some light on what's happening here?
>
> Cheers,
> Olav
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20121106/f9bfb727/attachment.html>


More information about the erlang-questions mailing list