[erlang-questions] Bad Argument, (Bug or something I'm missing).

38 views
Skip to first unread message

Chris Cook

unread,
Oct 1, 2012, 4:11:45 AM10/1/12
to erlang-q...@erlang.org
Morning list,

I have,

Erlang R15B01 (erts-5.9.1) [source] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.9.1 (abort with ^G)
1> List = "89234789234jhk2hk234892789fauky324978".
"89234789234jhk2hk234892789fauky324978"
2> <<List, <<"-">>/binary>>.
** exception error: bad argument

But when I write it as this,

3> <<"89234789234jhk2hk234892789fauky324978", <<"-">>/binary>>.
<<"89234789234jhk2hk234892789fauky324978-">>

I get the result I expected to get from the above 1 & 2. Could someone
please explain what is going wrong and why, because I'm very confused
with it.

Regards

Chris Cook.
_______________________________________________
erlang-questions mailing list
erlang-q...@erlang.org
http://erlang.org/mailman/listinfo/erlang-questions

Lukas Larsson

unread,
Oct 1, 2012, 4:43:53 AM10/1/12
to Chris Cook, erlang-q...@erlang.org
Hi,

If you just do

2> L = "a",<<L>>.
** exception error: bad argument

You get the same behaviour. When you do <<"a">> it is just syntactic
sugar for << <<97>>/binary >>. However when passing the value as a
variable you do not get the syntactic sugar help so you have to do
this:

3> L = "a",<<(list_to_binary(L))/binary>>.
<<"a">>

or something to that effect to get the correct result. Hope that makes sense.

Lukas

Jesse Gumm

unread,
Oct 1, 2012, 4:46:51 AM10/1/12
to Chris Cook, erlang-q...@erlang.org

Hi,

You just need to convert List to a binary first with list_to_binary.

The <<"something">> syntax is just a friendly way to make binary literals. But it does not perform automatic casting of lists.

Further, the extra << >> you're wrapping around "-" is unnecessary.

Example:

> L = "123456",
> B = list_to_binary(L),
> <<B/binary,"-">>.

Will yield:

<<"123456-">>

Hope that helps.

Take it easy,

-Jesse

--
Jesse Gumm
Owner, Sigma Star Systems
414.940.4866 || sigma-star.com || @jessegumm

Junior White

unread,
Oct 11, 2012, 12:20:28 AM10/11/12
to Jesse Gumm, erlang-q...@erlang.org
hi,
   This behave makes me confused too.

Joe Armstrong

unread,
Oct 11, 2012, 1:54:07 PM10/11/12
to Chris Cook, erlang-q...@erlang.org
1> L = "hello".
"hello"
2> << L, " world" >>.
** exception error: bad argument
3>
3> B = <<"hello">>.
<<"hello">>
4> << B, " world" >>.
** exception error: bad argument

5> << B/binary, " world" >>.
<<"hello world">>

<< X1 X2 ... >> constructs a binary. The X's can be literal strings
"....." or Var/Descriptor.

Its described in section 7.16 of the erlang reference manual

http://www.erlang.org/doc/reference_manual/expressions.html#id77949

Cheers

/Joe
Reply all
Reply to author
Forward
0 new messages