first and last return nothing on empty results#17054
Conversation
|
Note that with this change in place you can't distinguish [null] | try { last } catch { print "empty" }and now it will need to be [null] | if ($in | is-not-empty) { last } else { print "empty" } # will also collect, because of $inwhereas previously the [] | try { last } | default "n/a" |
|
but now you can do this, no [null] | last | default empty
empty
[] | last | default empty
empty |
|
If you didn't care about [] | default -e [null] | last
# or
[] | default -e [empty] | lastBut if you do care, you now can't tell if it was the last element that was |
I'm not trying to be rude, but why do I care to know if the last element was null or no elements at all. The point is to use it easily with default and replace what was there with a default value. BTW - I'm fine with changing this as long as it works the same way it does now. e.g. we can add parameters to default or first/last but I think the way this PR changes things should be the default way it works. |
If we follow that logic, then Previously, you had the option to do
If you mean 'now' as in 'on current main', that sounds a bit contradictory ("it's ok to change as long as nothing changes").
What kind of parameter do you have in mind? Opt-in to null return on out-of-bounds read (e.g. |
Let's try to add |
…empty list. (#17066) As title, this pr adds `--strict` flag to `first` and `last` command, so they will raise error on empty list. This is a follow up to #17054 ## Release notes summary - What our users need to know By default, `first` and `last` will return `null` on empty list, this pr is going to add `--strict` to make it returns on error. ``` > [] | first --strict Error: nu::shell::access_beyond_end × Row number too large (empty content). ╭─[entry #1:1:6] 1 │ [] | first --strict · ──┬── · ╰── index too large (empty content) ╰──── ``` ## Tasks after submitting NaN
This PR changes the behavior of
firstandlastwhen encountering empty results. This is done so that one can more easily usedefaultto provide a default value when results are empty. e.g.Release notes summary - What our users need to know
We changed the behavior of
firstandlastwhen encountering empty results. Previouslyfirstandlastwould error on an empty list. They now returnnullinstead.This is done so that one can more easily use
defaultto provide a default value when results are empty. e.g.Tasks after submitting