[Proposal] `IO.inspect_vars/1`

29 views
Skip to first unread message

Myron Marston

unread,
Oct 13, 2017, 11:44:20 PM10/13/17
to elixir-lang-core

IO.inspect has long been my go-to technique for troubleshooting code that isn’t quite working how I would expect. Since the :label option was added in 1.4, I use that all the time, too. The majority of the time, I use it to inspect one or more variables like so:

IO.inspect(foo, label: "foo")
IO.inspect(bar, label: "bar")

However, I often find my self making simple, dumb mistakes when setting this up, such as copying and pasting a prior IO.inspect line, but forgetting to update the label, e.g.:

IO.inspect(foo, label: "foo")
IO.inspect(bar, label: "foo") # woops, the output will label this as `foo`, too!

Given how common this pattern is, it would be really nice if there was a function or macro to do this for us. I’m thinking something like:

# inspect each variable in the current scope, labeling each with its name
IO.inspect_vars(:all)

# inspect just the provided variables, labeling each
IO.inspect_vars([foo, bar])

# inspect all variables in the current scope, except the provided ones, labeling each
IO.inspect_vars(except: [foo, bar])

I can also imagine it supporting a label option, to put a title above the list of variables, in order to distinguish multiple variable reports.

Would others find this as useful as I would? Is there interest in it being added to Elixir?

Thanks!
Myron

José Valim

unread,
Oct 14, 2017, 3:14:42 AM10/14/17
to elixir-l...@googlegroups.com
I use IO.inspect(binding()) a lot.

Although I don't think IO.inspect_vars would be a thing since it would need to be a macro and having to require it before would probably lose its appeal (and we don't plan to require anything other than Kernel by default).



José Valim
Founder and 
Director of R&D

--
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-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/fd0d67af-3b5f-4ee9-b067-8b263ccd1666%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Myron Marston

unread,
Oct 14, 2017, 3:21:07 AM10/14/17
to elixir-lang-core
I never knew about `binding/0`.  Thanks for the tip; `IO.inspect(binding())` definitely does the kind of thing I'm looking for.

--
You received this message because you are subscribed to a topic in the Google Groups "elixir-lang-core" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-lang-core/pzs8FVi7bdM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elixir-lang-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4KSmcNtff772p2vhepaqEHhTMuz0ntMU%2BX8U5L6Mukg3Q%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages