[erlang-questions] String versus variable in binary literal
Ian
hobson42@REDACTED
Wed May 16 11:15:06 CEST 2012
On 16/05/2012 09:49, Michel Rijnders wrote:
> Hi,
>
> Confused newbie here. Can someone please explain the following:
>
> 1> <<"foo">>.
> <<"foo">>
> 2> Foo = "foo".
> "foo"
> 3> <<Foo>>.
> ** exception error: bad argument
>
> What's the error in the last case?
>
> Best,
> Michel
Hi Michel,
As a slightly less confused (I hope) newbie....
<<"foo">> is a binary - and stored as 3 consecutive bytes.
"foo" is a list of characters, stored as the character "f" (in a word)
and an address (also a word) pointing to the list "oo" stored in a
similar manner. Yes, that does take 8 bytes of storage per character on
32 bit systems, and 16 bytes on 64 bit systems. Erlang was designed for
telephony, and not text processing!
You have to trigger the conversion yourself, using
list_to_binary(IoList) -> binary() - see
http://www.erlang.org/doc/man/erlang.html
Note that the parameter is an IoList - you can give list_to_binary a
comma separated list of lists and it will concatenate them for you. This
is more efficient than converting things as you go.
Regards
Ian
More information about the erlang-questions
mailing list