Skip to content
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

Closed
NotTheDr01ds opened this issue Oct 21, 2023 · 1 comment · Fixed by #10829
Closed

Can't match a null (but can using the deprecated $nothing) #10799

NotTheDr01ds opened this issue Oct 21, 2023 · 1 comment · Fixed by #10829
Labels
🐛 bug Something isn't working pattern-matching
Milestone

Comments

@NotTheDr01ds
Copy link
Contributor

NotTheDr01ds commented Oct 21, 2023

Describe the bug

match won't match a variable with null, 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

match $a {
    null => true,
    _ => false
}

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:

match $a {
    $nothing => true,
    _ => false
}

Returns true

Of course, the above is far oversimplified and would be better as an if statement (which does work):

if $a == null { true } else { false }

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
  • Or a number (float or int)

If $hours is 0 or null, then I want the distancePerHour result to be null as well, since the source values are coming from a Nu table where I'll be inserting the distancePerHour results.

Consider:

def distancePerHour [ distance: any hours: any ] {
    match $hours {
        0 => null,
        null => null,
        _ => ( $distance / $hours )
    }
}
  • distancePerHour 60 1 -> Works, returns 60
  • distancePerHour 60 0 -> Works, returns null
  • distancePerHour 60 null -> Fails with type mismatch for operator since the null 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):

def distancePerHour [ distance: any hours: any ] {
    match $hours {
        0 => null,
        $nothing => null,
        _ => ( $distance / $hours )
    }
}

Another workaround (but, IMHO, clunky) would be to default the null to some "magic" value:

def distancePerHour [ distance: any hours: any ] {
    let hours = ($hours | default "nada")
    match $hours {
        0 => null,
        "nada" => null,
        _ => ( $distance / $hours )
    }
}

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 and null.

Screenshots

No response

Configuration

key value
version 0.86.0
branch
commit_hash 5d8763e
build_os linux-x86_64
build_target x86_64-unknown-linux-gnu
rust_version rustc 1.71.1 (eb26296b5 2023-08-03)
rust_channel 1.71.1-x86_64-unknown-linux-gnu
cargo_version cargo 1.71.1 (7f1d04c00 2023-07-29)
build_time 2023-10-17 17:14:05 +00:00
build_rust_channel release
allocator mimalloc
features dataframe, default, extra, sqlite, static-link-openssl, trash, which, zip
installed_plugins
@NotTheDr01ds NotTheDr01ds added the needs-triage An issue that hasn't had any proper look label Oct 21, 2023
@amtoine 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
@amtoine
Copy link
Member

amtoine commented 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.
@hustcer hustcer added this to the v0.87.0 milestone Oct 25, 2023
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
Labels
🐛 bug Something isn't working pattern-matching
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants