You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
first, take, last, skip, drop, take until, take while, skip until, skip while, where, reverse, shuffle, append, prepend and sort-by should error when given non-lists instead of coercing to list #6941
This is a continuation of #6764. After reporting that where implicitly coerces non-lists to list, I decided to see how many other commands have this issue. As it turns out, the relevant commands I could find are: first, take, last, skip, drop, take until, take while, skip until, skip while, reverse, shuffle, append, prepend and sort-by. There may be more, too.
Now, the sheer scope of this issue leads me to believe that list-based commands coercing-to-list was originally a Nushell de facto standard or such. I have a couple of arguments against it, which I would like to present now:
Explicitly wrapping a non-list into a list is not hard
To make a known non-list value in a pipeline into a list, simply write [$in]. It's that easy. I will admit that currently, there's no really quick way of wrapping an unknown non-list value but not an existing list, apart from [$in] | flatten, but it's still possible. Having this kind of wrapping feature embedded, with no opt-out, in over a dozen commands is highly unnecessary.
Masks errors
If something that should receive a list (and cannot do anything meaningful with a non-list) does not receive a list, then it should report an error as soon as possible. This is the basis of a useful type system - it helps the user quickly comprehend the meaning of the commands, and helps prevent mistakes when writing complicated pipelines. All of the commands above only have meaning when applied to lists.
Promotes plain records into tables
This is a rather sharp corner for new users - it's difficult to tell the difference between records and one-row tables if you don't already know what one or the other "should" look like, and having a bunch of commands which convert one into the other if you use them improperly does not help matters in the slightest.
Now, the sheer scope of this issue leads me to believe that list-based commands coercing-to-list was originally a Nushell de facto standard or such.
After looking into the implementation of some of the commands, I think this behaviour was largely an accident. A lot of commands blindly call into_iter() on the pipeline input:
As I understand it into_iter()/IntoIterator is designed to be infallible, and so it always returns something even when operating on scalar PipelineData+Value variants. Not a great choice for these commands.
When we do work on this, we should probably introduce a a -> [a] function called enlist or unflatten so facilitate piping an arbitrary value into a list without having to wrap it around. Ideally, maybe 1 | [] should do that, but treating values [] as functions is not a style common to Nu.
Workarounds:
We could just pipe: 1 | do {|x| [x]} but this is a bit verbose to type out loud.
We could define def enlist [...cols] {[$in]} and do 1 | enlist
We could also do 1 | prepend [] and requires no additional changes. We should probably add documentation for flatten and prepend [] to show the inverse relation.
Thirteen of the commands listed here error on non-list input by now (#7123, #7623). append and prepend still accept a single value.
That got reverted on purpose in #7660 and is part of their signature (any -> list<any>).
Activity
rgwood commentedon Nov 7, 2022
Thank you for looking into this!
I agree that these commands should error when given a non-list input.
WindSoilder commentedon Nov 13, 2022
I think we can make a check list to check resolved command, what do you think? It's unlikely to solved by a simple pr
rgwood commentedon Nov 13, 2022
Fixed for
takeandfirstin #7123.rgwood commentedon Nov 13, 2022
After looking into the implementation of some of the commands, I think this behaviour was largely an accident. A lot of commands blindly call
into_iter()on the pipeline input:nushell/crates/nu-command/src/filters/reverse.rs
Lines 70 to 75 in 336df6c
As I understand it
into_iter()/IntoIteratoris designed to be infallible, and so it always returns something even when operating on scalarPipelineData+Valuevariants. Not a great choice for these commands.last, skip, drop, take until, take while, skip until, skip while, whe…
rgwood commentedon Jan 3, 2023
We've temporarily reverted the fix for
appendandprependwhile we sort out some issues with PATH andenv.nu: #7623 (comment)hyiltiz commentedon Jul 5, 2023
When we do work on this, we should probably introduce a
a -> [a]function calledenlistorunflattenso facilitate piping an arbitrary value into a list without having to wrap it around. Ideally, maybe1 | []should do that, but treating values[]as functions is not a style common to Nu.Workarounds:
1 | do {|x| [x]}but this is a bit verbose to type out loud.def enlist [...cols] {[$in]}and do1 | enlist1 | prepend []and requires no additional changes. We should probably add documentation forflattenandprepend []to show the inverse relation.kronberger-droid commentedon Nov 13, 2026
Thirteen of the commands listed here error on non-list input by now (#7123, #7623).
appendandprependstill accept a single value.That got reverted on purpose in #7660 and is part of their signature (
any -> list<any>).