Elixir: IO.inspect in iex incorrectly displays elements in a list

Created on 3 Apr 2016  路  3Comments  路  Source: elixir-lang/elixir

IO.inspect in iex incorrectly displays elements in a list, particularly elements that contains identical pairs of certain numbers. See example below.

http://elixirplayground.com?gist=4e34a25eb7471b386596a16c9b6fd3af

Most helpful comment

@vothane because Erlang's "strings" are char lists (lists of integers), which Elixir needs to support, that ambiguity will always exist and Inspect is simply doing a best effort job of trying to display the right thing. You can force it to display the list of integers using this:

iex> inspect [10, 10], char_lists: :as_lists
"[10, 10]"

When inspect output seems weird, you can use the i helper which should provide you with more information. In this case, it's:

iex> i [10, 10]
Term
  '\n\n'
Data type
  List
Description
  This is a list of integers that is printed as a sequence of characters
  delimited by single quotes because all the integers in it represent valid
  ASCII characters. Conventionally, such lists of integers are referred to as
  "char lists" (more precisely, a char list is a list of Unicode codepoints,
  and ASCII is a subset of Unicode).
Raw representation
  [10, 10]
Reference modules
  List

All 3 comments

This is not IO.inspect/1 incorrectly displaying elements in those lists; it's that lists of integers such as [10, 10], where all integers can be interpreted as codepoints for printable characters (10 would be \n) are printed as such in Elixir.

You can get more information on the lists you inspected by doing i list1 in IEx. Also, a bunch of useful information related to this subject can be found in the "Binaries, strings and char lists" chapter of the Getting Started guide.

@vothane because Erlang's "strings" are char lists (lists of integers), which Elixir needs to support, that ambiguity will always exist and Inspect is simply doing a best effort job of trying to display the right thing. You can force it to display the list of integers using this:

iex> inspect [10, 10], char_lists: :as_lists
"[10, 10]"

When inspect output seems weird, you can use the i helper which should provide you with more information. In this case, it's:

iex> i [10, 10]
Term
  '\n\n'
Data type
  List
Description
  This is a list of integers that is printed as a sequence of characters
  delimited by single quotes because all the integers in it represent valid
  ASCII characters. Conventionally, such lists of integers are referred to as
  "char lists" (more precisely, a char list is a list of Unicode codepoints,
  and ASCII is a subset of Unicode).
Raw representation
  [10, 10]
Reference modules
  List

This is by far the "bug" (actually a feature) that gets more times being misreported
Linking to the FAQ hoping it will help avoid misreports in the future https://github.com/elixir-lang/elixir/wiki/FAQ#4-why-is-my-list-of-integers-printed-as-a-string

Was this page helpful?
0 / 5 - 0 ratings