Add :label option to IO.inspect/2#5010
Conversation
There was a problem hiding this comment.
"of formatting options" -> "of the remaining formatting options"?
f377709 to
139e78f
Compare
There was a problem hiding this comment.
We use #=> for return values, so those should be reversed or something.
|
@whatyouhide I tried improving the docs for whole |
There was a problem hiding this comment.
Awesome with the before/after example, I love it. About the format though, what if we do:
@doc """
...
## Examples
IO.inspect <<0, 1, 2>>, width: 40
# Prints:
# <<0, 1, 2>>
IO.inspect 1..100, tag: "a wonderful range"
# Prints:
# a wonderful range: 1..100
without showing the return value (which is what we already do?
e07fe65 to
904194f
Compare
There was a problem hiding this comment.
We don't say this in other docs. It's structured like:
expression
#=> output
There was a problem hiding this comment.
Also this Prints: looks like part of actual output.
There was a problem hiding this comment.
Well, that's confusing: #5010 (comment)
We use
#=>for return values, so those should be reversed or something.
There was a problem hiding this comment.
@lexmag @whatyouhide maybe something like this will be clearer:
[1, 2, 3]
|> IO.inspect(tag: "before")
|> Enum.map(&(&1 * 2))
|> IO.inspect(tag: "after")
|> Enum.sum
Prints:
# before: [1, 2, 3]
# after: [2, 4, 6]
There was a problem hiding this comment.
I think we had same discussion when warn function was introduced and decided to go with #=> until we find a better option.
@michalmuskala that Prints: now looks like a part of the code, what if we inline this into comment like:
[1, 2, 3]
|> IO.inspect(tag: "before") # prints "before: [1, 2, 3]"
|> Enum.map(&(&1 * 2))
|> IO.inspect(tag: "after") # prints "after: [2, 4, 6]"
|> Enum.sum?
There was a problem hiding this comment.
I am definitely 👎 on using #=> because we use if for return values (and we should fix IO.warn/2 and friends as well). I like @michalmuskala's suggestion of showing the whole snippet of code (with the return value of the whole pipe after #=> maybe) and then something like:
"""
[1, 2, 3]
|> ...
#=> ...
Prints:
before: [1, 2, 3]
after: [2, 4, 6]
"""There was a problem hiding this comment.
Another option is to not doctest this module but keep examples exactly as users would see in the terminal. For example:
iex> [1, 2, 3] |>
...> IO.inspect(tag: "before") |>
...> Enum.map(&(&1 * 2)) |>
...> IO.inspect(tag: "after") |>
...> Enum.sum
before: [1, 2, 3]
after: [2, 4, 6]
12
There was a problem hiding this comment.
@josevalim that would work but we would lose the doctests (even if that may be ok, in general it's not good I think 😕) and also still not have a solution for this. I think the two separate blocks of code may be good here.
There was a problem hiding this comment.
Two separate blocks are good option I think 👍.
|
So @michalmuskala, to recap:
and we should be good to go :) |
3931dcb to
226ef51
Compare
|
@whatyouhide ready for review |
|
@michalmuskala now that I'm seeing it in action, I think we should drop the |
226ef51 to
900e3eb
Compare
|
@whatyouhide removed |
|
This looks fantastic to me, and ready to merge :) @josevalim do I |
There was a problem hiding this comment.
"This makes it possible to "spy" on values by inserting an IO.inspect/2 call almost anywhere in your code, even in the middle of a pipeline.", might be better phrasing?
There was a problem hiding this comment.
I don't like the "even" like using IO.inspect/2 in a pipeline was special, but 👍 on the rest of the phrasing, may be a bit better :) But overall I think we're ok to merge, these are mostly details for now, otherwise we're gonna be torturing @michalmuskala with fixes over fixes 😄
There was a problem hiding this comment.
It's not bad as is, but currently this does not read as idiomatic English, imo. :) Just figured this is some documentation people new to the language might see frequently, so it should be as good as possible.
There was a problem hiding this comment.
This makes it possible to "spy" on values by inserting an
IO.inspect/2 call almost anywhere in your code, for example,
in the middle of a pipeline.
|
I love this. Would anyone be in support of adding a short name for the |
|
@antipax strong 👎 for my side for |
|
@whatyouhide that's all quite fair. :) The idea behind the short version was being able to add inspects quickly, but label isn't particularly long either. :) |
900e3eb to
c8af0c5
Compare
|
@antipax @whatyouhide updated |
|
Looks great to me. Let's wait for the official seal of approval™ :) |
There was a problem hiding this comment.
Else should probably use an empty list instead of an empty binary. It is a small detail but since we are working with iodata, that makes more sense as an empty value.
There was a problem hiding this comment.
Seems we're actually working with chardata here, and it would be better to rename iodata accordingly.
There was a problem hiding this comment.
So let's rename iodata below to chardata and replace "" with [] @michalmuskala?
|
Looks good to me! |
This option will decorate the output with additional string, so
it's easy to distinguish between different IO.inspect calls.
IO.inspect("foo", label: "bar")
=> bar: "foo"
c8af0c5 to
43201c7
Compare
|
@whatyouhide updated |
|
Beautiful, thanks @michalmuskala 💟 I already love this feature :D |
This option will decorate the output with additional string, so
it's easy to distinguish between different IO.inspect calls.
Closes #5009