Binary syntax

I can do this:

iex(22)> <<"hello", <<>> >>
"hello"

But not this:

iex(23)> x = <<>>
""

iex(24)> <<"hello", x >>
** (ArgumentError) argument error

Why?

From Kernel.SpecialForms documentation.

Variables or any other type need to be explicitly tagged:

This would have worked

iex(1)> x = <<>>
iex(2)> <<"hello", x::binary >>              
"hello"

You can see the difference with

iex(3)> quote do <<"hello", x >> end
{:<<>>, [], ["hello", {:x, [], Elixir}]}
iex(4)> quote do <<"hello", x::binary >> end 
{:<<>>, [], ["hello", {:::, [], [{:x, [], Elixir}, {:binary, [], Elixir}]}]}
5 Likes

Thanks!