Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why `<< String.reverse("Halo") >>` is not allowed in Elixir?

Tags:

elixir

Why is this OK,

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

while this is not OK in Elixir?

iex(24)> << String.reverse("Halo") >>
** (ArgumentError) argument error
like image 376
Andree Avatar asked Dec 22 '25 09:12

Andree


1 Answers

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"
like image 57
Dogbert Avatar answered Dec 24 '25 11:12

Dogbert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!