List bug?

Hello.
My co-worker found a strange behavior of list.

iex(3)> defmodule User do
...(3)>     defstruct id: 0,name: "man"
...(3)> end
{:module, User,
 <<70, 79, 82, 49, 0, 0, 6, 192, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 181,
   0, 0, 0, 18, 11, 69, 108, 105, 120, 105, 114, 46, 85, 115, 101, 114, 8, 95,
   95, 105, 110, 102, 111, 95, 95, 10, 97, ...>>, %User{id: 0, name: "man"}}
iex(4)> defmodule ListError do
...(4)>   alias User
...(4)>   def list_error do
...(4)>     [
...(4)>       %User{id: 11,name: "Ash"},
...(4)>       %User{id: 13,name: "Pap"},
...(4)>       %User{id: 12,name: "Bob"}
...(4)>     ]
...(4)>     |>Enum.map(fn x -> x.id end)
...(4)>     |>IO.inspect
...(4)>   end
...(4)> end
{:module, ListError,
 <<70, 79, 82, 49, 0, 0, 6, 184, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 205,
   0, 0, 0, 21, 16, 69, 108, 105, 120, 105, 114, 46, 76, 105, 115, 116, 69, 114,
   114, 111, 114, 8, 95, 95, 105, 110, 102, ...>>, {:list_error, 0}}
iex(5)> ListError.list_error
'\v\r\f'
'\v\r\f'

I expected [11, 13, 12], but it shows a character with a backslash.
Additionally, my iex says the characters are list.

iex(6)> is_list('\v\r\f')
true

and

iex(11)> Enum.each('\q\w\ae',fn x -> IO.inspect x end)
113
119
7
101
:ok 

What is happening inside iex?
Give me an opinion!

This is not a bug, but a charlist:
https://hexdocs.pm/elixir/List.html#module-charlists

Why Enum.map returns charlist?

I linked to the documentation, which starts with:

If a list is made of non-negative integers, where each integer represents a Unicode code point, the list can also be called a charlist. These integers must:

  • be within the range 0..0x10FFFF ( 0..1_114_111 );
  • and be out of the range 0xD800..0xDFFF ( 55_296..57_343 ), which is reserved in Unicode for UTF-16 surrogate pairs.

The result of your mapping does match that specification.

2 Likes

Thank you for the prompt answer!