[Proposal] - Add IO.inspect/2 with a binary as the second argument

140 views
Skip to first unread message

Faisal Alghurayri

unread,
Apr 21, 2021, 11:35:26 PM4/21/21
to elixir-lang-core
Hello friends,

Thank you for your great work in developing and maintaining Elixir. 🙌

I propose adding another signature to the IO.inspect/2 function where the second argument is a binary:

IO.inspect(something, "This message should get shown in console")

I think this is a small quality of life improvement since the developer won't need to explicitly pass the `label` option to see an associated message.

Best Regards,
Faisal

Allen Wyma

unread,
Apr 21, 2021, 11:42:53 PM4/21/21
to elixir-l...@googlegroups.com
There's already a label option in the docs:

IO.inspect(1..100, label: "a wonderful range")

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/64793786-deea-4902-b450-39f53896c945n%40googlegroups.com.

Faisal Alghurayri

unread,
Apr 21, 2021, 11:56:09 PM4/21/21
to elixir-lang-core
Yes. 👍 

I'm proposing:

def inspect(item, message) when is_binary(message)  do

inspect(:stdio, item, label: message)

end


The reason is just a small quality of life improvement. 😄 Please let me know what you think!

- Faisal

Austin Ziegler

unread,
Apr 22, 2021, 10:14:59 AM4/22/21
to elixir-l...@googlegroups.com
Every time I forget to add the `label: ` and get a compile error (which is often), I agree with you. From a developer consistency perspective, though, I’m not as sold on this. With this convenience, people may forget to switch back to the keyword list when they need to add other options like :limit, and try `IO.inspect(something, "Message", limit: 300)`.

-a



--

Christopher Keele

unread,
Apr 22, 2021, 2:15:15 PM4/22/21
to elixir-lang-core
> From a developer consistency perspective, though, I’m not as sold on this.

I tend to agree, this feels like nice parameter sugar but makes it harder to interface consistently with extended options. Maybe it makes more sense for an application-level helper?

Faisal Alghurayri

unread,
Apr 23, 2021, 2:34:56 AM4/23/21
to elixir-lang-core
Thanks for your feedback folks. I see where you are coming from and I can not disagree. 😄

> Maybe it makes more sense for an application-level helper?

Correct. I will do that going forward. 👍

- Faisal

Leandro Cesquini Pereira

unread,
Apr 23, 2021, 10:24:26 AM4/23/21
to elixir-lang-core
Using an atom instead of a string will save you some time: IO.inspect(foo, label: :my_var)

Not so direct as an app-level helper but easier to write than a string :)

nico

unread,
Apr 23, 2021, 10:52:21 AM4/23/21
to elixir-l...@googlegroups.com
I often do: `IO.inspect(this_is_my_label: my_var)`

The output will be wrapped in a list, but otherwise it is the same as using the label option, without using the second argument.

- Nico Piderman


Aaron Ross

unread,
Apr 23, 2021, 12:10:04 PM4/23/21
to elixir-l...@googlegroups.com
You can save a few more keystrokes by just adding a label to the inspect payload:

    IO.inspect(my_var: foo)

I end up inspecting a map pretty often, when I want to see multiple values at once

    IO.inspect(%{foo: foo, bar: bar})

--

Christopher Keele

unread,
Apr 23, 2021, 1:32:25 PM4/23/21
to elixir-lang-core
> Thanks for your feedback folks.

Thanks for your proposal, discussions like these are what help keep the language evolving towards better user-friendliness!

Austin Ziegler

unread,
Apr 27, 2021, 1:58:44 PM4/27/21
to elixir-l...@googlegroups.com
I do the same when I’m using a standalone `IO.inspect`, but the proposal here would help most with a _pipelined_ inspect (`foo |> IO.inspect("label")`).

I’m still not sure that the positive of using a string to avoid compile errors outweighs the inconsistency and resulting complexity, but that’s where I would want it because that’s where I have this problem most frequently.

-a

Reply all
Reply to author
Forward
0 new messages