markhern

markhern

The below code is returning “Here’s my list: efl” for me, substituting the ASCII numeric equivalents for 101, 102, and 108. Does anyone feel that this is a bug that should be reported? If I add a higher number like 4000 to the :top key, then Keyword.get_values() works correctly. Looks like a bug to me. I’m running Elixir 1.10.3.

defmodule Trythis do
    def findit(jay) do
        hh = Keyword.get_values(jay, :top)
        IO.puts("Here's my list: #{IO.inspect(hh)}")
    end
end

kl = [top: 101, height: 201, bottom: 22, top: 102, bottom: 40, top: 108]
Trythis.findit(kl)

Showing Posts 1 to 10

dimitarvp

dimitarvp

You still have the numbers in place, it’s just that IO.puts and IO.inspect see those integers are printable ASCII characters so they choose to print them as characters.

If you want it not to do that, try this code:

IO.inspect(hh, charlists: :as_lists, label: "Here's my list")

The original behaviour is not a bug, simply a default convenience which you can opt out from.

NobbZ

NobbZ

It should return :ok, as thats the only value that IO.puts/2 is able to return.

It my print Here's my list: efl though. But also it should print 'efl', as a sideffect of calling IO.inspect.

If you want to display a list of integers always as a list of integers, you can use List.to_string/1.

markhern

markhern OP

Yes, my use of IO.inspect was incorrect. But you do see that Keyword.get_values() is returning chars unless I replace 108 with 400 and then its all numbers as we’d expect. Isn’t this inconsistent?

iex(1)> kl = [top: 101, height: 201, bottom: 22, top: 102, bottom: 40, top: 108]
[top: 101, height: 201, bottom: 22, top: 102, bottom: 40, top: 108]
iex(2)> Keyword.get_values(kl, :top)
'efl'
NobbZ

NobbZ

No, it is how inspection works.

Any list that only consist of numbers in “printable” ASCII range (10 to 127 iirc) are inspected as charlist by default.

You still can treat it as list of numbers anywhere, as it is a list of numbers.

It’s just representation, it’s not a change in value or type.

Ps: Treachery of Representation - Hauleth not directly about charlists, but in general the same problem

mudasobwa

mudasobwa

Creator of Cure
'efl' == [101, 102, 108]
#⇒ true
markhern

markhern OP

Yes, its just representation but why does Keyword.get_values(kl,:bottom) return a list of numbers? Its just inconsistent – and does Keyword.get() ever return chars? This is not the robustness I expect of a mature language. I’m aware that the Elixir type system is different and that this behavior isn’t inconsistent in that light.

NobbZ

NobbZ

Both return a list of numbers, which in one case fullfils a given predicate and in one case not.

The predicate is Enum.all(list_to_inspect, &List.ascii_printable?/1), if this returns true then the list is inspected as a charlist, if it returns false it is represented as a list of integers.

PS: Keyword.get/3 never returns a “char”, it returns whatever the value is.

markhern

markhern OP

To be clear, I want numbers to appear. Unfortunately, even with charlists: :as_lists I still get ‘efl’ from the IO.inspect(). Are you not getting the same? Is my only option to include a 0 in the list? Why does print formatting have to be so hard – haven’t we already figured this one out (in other languages)?

NobbZ

NobbZ

In what exact case?

For me it works…

iex(1)> IO.inspect ~c'efl', charlists: :as_lists
[101, 102, 108]
'efl'

Where the first line is the actual printed value by IO.inspect and the second line is the inspection of its returned value.

Though if you want to present a list of numbers to a user, you shouldn’t rely on IO.inspect or the Inspect protocol at any time. Its meant for debugging and it might chance how it represents individual values without notice!

If you need reliable output use one of the various to_string functions from the various modules as you need them.

markhern

markhern OP

I’m trying to get a numeric list to appear using string interpolation (as in the code of my original post). So, even using the option of charlists: :as_lists in IO.inspect will not yield the numeric list I so humbly wish for. While IO.inspect returns the numeric list, it’s printing ‘efl’.

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
thiagogsr
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews