2
votes

Why is this OK,

iex(23)> << "Halo" >>
"Halo"

while this is not OK in Elixir?

iex(24)> << String.reverse("Halo") >>
** (ArgumentError) argument error
1

1 Answers

8
votes

When you have an expression inside << >>, the type of that expression is assumed to be an integer representing one byte by default.

iex(1)> << trunc(65.2) >>
"A"
iex(2)> << trunc(1000.3) >>
<<232>>

If your expression is a binary (also called a String in Elixir), you need to specify the type of the expression explicitly:

iex(3)> << String.reverse("Halo")::binary >>
"olaH"