[erlang-questions] Re: binary expression with fixed and variablefields

Robert Virding rvirding@REDACTED
Fri Jul 2 03:13:51 CEST 2010


The main reason why 4> isn't valid is that the default type of a
binary segment is an integer. So by not qualifying A you are trying to
match an integer from ABC with the *binary* <<"a">>, which can never
match. That 1> works is because a string literal in a binary is
expanded into the sequence of bytes in the string. So in effect 1> is
equivalent to

<<$a,_/binary>> = <<$a,$b,$c>>.

Robert

On 2 July 2010 02:58, Steve Davis <steven.charles.davis@REDACTED> wrote:
> On a related note, I find it curious that there's the following
> limitation forcing you to manually calculate the size of an already
> known binary:
>
> 1> <<"a", _/binary>> = <<"abc">>.
> <<"abc">>
> 2> A = <<"a">>.
> <<"a">>
> 3> ABC = <<"abc">>.
> <<"abc">>
> 4> <<A, _/binary>> = ABC.
> ** exception error: no match of right hand side value <<"abc">>
> 5> Size = size(A).
> 1
> 6> <<A:Size/binary, _/binary>> = ABC.
> <<"abc">>
>
> ...a minor inconvenience, but irritating.
>
> Perhaps there's a reason that I don't immediately see why 4> isn't
> valid?
>
> /s
>
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
>
>


More information about the erlang-questions mailing list