[erlang-questions] String versus variable in binary literal
Wed May 16 12:44:17 CEST 2012
On 05/16/2012 12:29 PM, Joe Armstrong wrote:
> On Wed, May 16, 2012 at 10:56 AM, Richard Carlsson
> <> wrote:
>> On 05/16/2012 10:49 AM, Michel Rijnders wrote:
>>> Confused newbie here. Can someone please explain the following:
>>> 1> <<"foo">>.
>>> 2> Foo = "foo".
>>> 3> <<Foo>>.
>>> ** exception error: bad argument
>>> What's the error in the last case?
>> The bit syntax doesn't (currently) support encoding strings that are not
>> constant literals. This is something that should be fixed, IMO.
> It's a bug (or should be a bug) - try this for size:
> Isn't that beautiful :-)
Beautiful or not, please let's not mix two completely different issues
that are here.
There is no string constant in Joe's example. There is a binary with
three segments. And for better or worse, the default binary segment
when one does not specify the type and size of a segment is a *byte*
(type=integer, size=8). This is clearly stated in the language
reference manual in a very visible green box:
When constructing binaries, if the size N of an integer segment is
too small to contain the given integer, the most significant bits of
the integer will be silently discarded and only the N least
significant bits will be put into the binary.
More information about the erlang-questions