Get back charlist when maping list

I often get back charlist when mapping lists of structs in ecto, and for the life of me I cant figure out how to reproduce this consistently much less how to fix it.

Usually happens when i do something like list_of_structs |> Enum.map(fn s -> s.id end), where would expect this to return a list of integers here. The charlist is usually random, something like 'dsafsgfdgfdbfdg'

Has anyone experienced this and know a good way to resolve?

Thanks!

iex(1)> IO.inspect 'dsafsgfdgfdbfdg', charlists: :as_lists
[100, 115, 97, 102, 115, 103, 102, 100, 103, 102, 100, 98, 102, 100, 103]
'dsafsgfdgfdbfdg'

I.e. a list of integers within a certain range is interpreted as a charlist.

Charlists:

If a list is made of non-negative integers, it can also be called a charlist. Elixir uses single quotes to define charlists.
In particular, charlists may be printed back in single quotes if they contain only ASCII-printable codepoints.
The rationale behind this behaviour is to better support Erlang libraries which may return text as charlists instead of Elixir strings.
A list can be checked if it is made of printable ascii codepoints with ascii_printable?/2.

2 Likes

interesting, thanks for the reply!