[erlang-questions] Bit syntax decscription in erlang reference manual?
Edwin Fine
erlang-questions_efine@REDACTED
Fri Jun 20 08:04:39 CEST 2008
Sounds right to me.
One little thing, though: the newer versions of Erlang support a type,
"bytes", which is identical to binary, but a more accurate description.
There is also a size_bytes() function. As I understand it, we are encouraged
to phase out the use of binary in favor of bytes.
So the example could have been written with a more "modern" syntax as:
<<A:1/bytes,B/bytes>> = <<1,2,3>>.
S = byte_size(B).
Example:
10> <<A:1/bytes,B/bytes>> = <<1,2,3>>.
<<1,2,3>>
11> S = byte_size(B).
2
12> A.
<<1>>
13> B.
<<2,3>>
Hope this helps.
On Fri, Jun 20, 2008 at 12:00 AM, 成立涛 <litaocheng@REDACTED> wrote:
> yeah. I see. Thank you very much Edwin.
> my opinion: in binary syntax, the integer, float can always use their
> default size, but binary(bits and bytes) must declare its size specification
> except the very last binary element. Is't right?
> I come from china, so my english is not good, please don't mind my
> expression. :)
>
> 2008/6/20 Edwin Fine <erlang-questions_efine@REDACTED>:
>
> There's a subtlety here that you may have missed. The manual says
>>
>> All other *bit string or binary* elements in the matching must have a size
>> specification.
>>
>> In <<A, B, C>> = <<1,2,3>>, all elements on the left hand side are by
>> default of type integer, not bitstring or binary. Therefore sizes are
>> assumed to be 1 byte each.
>>
>> Your example is the same as writing
>>
>> <<A/integer, B/integer, C/integer>> = <<1,2,3>>.
>>
>> This below, for example, would NOT work:
>>
>> <<A/binary,B/binary>> = <<1,2,3>>.
>>
>> You would have to write
>>
>> <<A:1/binary,B/binary>> = <<1,2,3>>.
>>
>> Example:
>> 1> <<A/binary,B/binary>> = <<1,2,3>>.
>> * 1: a binary field without size is only allowed at the end of a binary
>> pattern
>> 2> <<A:1/binary,B/binary>> = <<1,2,3>>.
>> <<1,2,3>>
>> 3> A.
>> <<1>>
>> 4> B.
>> <<2,3>>
>> 5>
>>
>> Hope this helps.
>> 2008/6/19 成立涛 <litaocheng@REDACTED>:
>>
>>> In erlang reference mannual, the Bit Syntax Expressions section, about
>>> the binary Size, the manual said:
>>> "In matching, this default value is only valid for the very last element.
>>> All other bit string or binary elements in the matching must have a size
>>> specification."
>>> we know the default size for integer is 8, float is 64, binary is the
>>> whole bytes.
>>> for example:
>>> > <<A, B, C>> = <<1, 2, 3>>.
>>> I think in this pattern match, the variables A, B, C all use the dafault
>>> Size.
>>> Is it a conflict with reference manual?
>>> thanks!
>>>
>>> _______________________________________________
>>> erlang-questions mailing list
>>> erlang-questions@REDACTED
>>> http://www.erlang.org/mailman/listinfo/erlang-questions
>>>
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080620/7718d3b5/attachment.htm>
More information about the erlang-questions
mailing list