-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't match
a null
(but can using the deprecated $nothing
)
#10799
Labels
Milestone
Comments
amtoine
added
🐛 bug
Something isn't working
pattern-matching
and removed
needs-triage
An issue that hasn't had any proper look
labels
Oct 21, 2023
not sure it's in our issue tracker already but i'm quite sure i've seen that somewhere, good catch 🙏 |
hudclark
added a commit
to hudclark/nushell
that referenced
this issue
Oct 24, 2023
hudclark
added a commit
to hudclark/nushell
that referenced
this issue
Oct 24, 2023
hudclark
added a commit
to hudclark/nushell
that referenced
this issue
Oct 24, 2023
WindSoilder
pushed a commit
that referenced
this issue
Oct 24, 2023
# Description Support pattern matching against the `null` literal. Fixes #10799 ### Before ```nushell > match null { null => "success", _ => "failure" } failure ``` ### After ```nushell > match null { null => "success", _ => "failure" } success ``` # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> Users can pattern match against a `null` literal.
hardfau1t
pushed a commit
to hardfau1t/nushell
that referenced
this issue
Dec 14, 2023
# Description Support pattern matching against the `null` literal. Fixes nushell#10799 ### Before ```nushell > match null { null => "success", _ => "failure" } failure ``` ### After ```nushell > match null { null => "success", _ => "failure" } success ``` # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> Users can pattern match against a `null` literal.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Describe the bug
match
won't match a variable withnull
, but will match using$nothing
(deprecated) as the pattern.Ran into this in a real-world use-case that's a bit complex. Took me the better part of a day (on and off) to figure out the root cause and get it into some minimal reproducible examples. At the most basic:
How to reproduce
Outputs
false
Expected behavior
Outputs
true
Additional context
Note that this MRE works if matching against
$nothing
, but this workaround will fail as of 0.87 when that is removed:Returns
true
Of course, the above is far oversimplified and would be better as an
if
statement (which does work):My real world use-case is that I have a calc involving two values. Consider it something like
distancePerHour
. My$hours
variable may be:null
0
If
$hours
is 0 or null, then I want thedistancePerHour
result to benull
as well, since the source values are coming from a Nu table where I'll beinsert
ing thedistancePerHour
results.Consider:
distancePerHour 60 1
-> Works, returns 60distancePerHour 60 0
-> Works, returnsnull
distancePerHour 60 null
-> Fails withtype mismatch for operator
since thenull
didn't match and it fell through to the catch-all(Side-note: As a workaround, yes, I could massage the data in advance, but that's not as "clean")
Keep in mind that the following works as expected (for now):
Another workaround (but, IMHO, clunky) would be to
default
thenull
to some "magic" value:Of course, for this MRE, I could also
default
to 0, but that only works if the desired match result is the same for both 0 andnull
.Screenshots
No response
Configuration
The text was updated successfully, but these errors were encountered: