-
Notifications
You must be signed in to change notification settings - Fork 8k
Add -is $null and -isnot $null support to operators and Where-Object #10704
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
Conversation
@KirkMunro should we provide support here also for the null-like values Given the decisions in #9561, I would say that we probably should have this operation also treat the null-representatives as null via /cc @daxian-dbw |
@PoshChan Please restart macos |
@KirkMunro, successfully started retry of |
@vexx32 So you're proposing this update invoke Aside: |
@KirkMunro yep, that's the conclusion we came to in #10422 which is currently stalled pending a question on how we determine equality and what matters to it when using |
src/System.Management.Automation/engine/runtime/Operations/MiscOps.cs
Outdated
Show resolved
Hide resolved
I'd expect consistency with |
@iSazonov Are you're suggesting that we should support When using $x = [AutomationNull]::Value
$x -is [AutomationNull] # returns true
$x -is [AutomationNull]::Value # returns an error because the right-hand side is not a type or null I believe the above behavior is desirable and expected. The only outstanding question I have on this PR is whether or not |
@KirkMunro i would that that if given the type itself to test, it would follow current behaviour, and if given a null singleton value, it should follow the IsNullLike pattern. Reason being is that |
null is not type too. So it is under question too. If we allow null then we should allow [AutomationNull]::Value too. Or disable null. $x="abc"
$x -is "abc" # -> True |
Using the The other nulls already work using Your question @iSazonov highlights exactly why I think we shouldn't support The answer to that question is simple: intent/ambiguity.
But what about the following syntax:
If you want to fall back to By forcing tests for value equality to be done using the |
@vexx32 To your point, I think it makes sense when checking for |
Is the final proposal then that:
|
@rjmholt That's almost 100% correct.
The other statements you listed were accurate, and the code and tests were just updated to support that proposal. |
For consistency we could define [Null] type. |
@KirkMunro sorry, was using the type name as a shorthand for the singleton value. Ok, that sounds good to me. I certainly agree that not allowing the alt-null values on the RHS is the way to go. |
This comment has been minimized.
This comment has been minimized.
This PR is still awaiting a decision or a merge. |
This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days. |
I know this has already been discussed, but I want to register that I'm worried about a cmdlet having a different behaviour between the This PR does great work, and I think the addition of
I'm not sure I fully agree that the breaking change that the literal As I say, I think I just want to make these points in writing so that maintainers or the @PowerShell/powershell-committee can review the design carefully. |
@PowerShell/powershell-committee reviewed this, we prefer adding |
@SteveL-MSFT That would effectively make it the only postfix operator currently existing in PS, at least if we're still talking I'd be concerned about whether that would overcomplicate the parsing logic for operators. |
This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days. |
We could make it a regular prefix unary operator with a # Expressions
-isnull $var
-isnotnull $var
# Where-Object
... | Where-Object -isnull Foo
... | Where-Object -isnotnull Foo
... | Where-Object -isnotnull # once #8357 gets implemented |
I stand corrected. 🙂 |
It works with the unary PS> [pscustomobject] @{ foo = 0 } | Where-Object -Not foo
foo
---
0 Note that, for the reasons mentioned, |
My only objection to any of the arguments made above and to what the Committee decided on is that we should have I also feel strongly that these should be postfix operators, not prefix operators so that the expressions can be read and more easily understood, so I'm going to move forward with that approach. Part of the value with these changes is eliminating the need to put the $array -isnull # Intuitive, reads exactly like what it checks
-isnull $array # Not as readable I've never liked that PSScriptAnalyzer always complains and tells me to put my |
@vexx32: You're forgetting about the |
I can see the appeal of postfix However, I still think that I definitely understand @rjmholt's concern about the special-casing of only supporting a verbatim However, he also states:
So my plea is: Let us stick with I think there's a reasonable case to be made that the technically breaking change that this constitutes falls into Bucket 3: Unlikely Grey Area:
|
Closing. See #10238 (comment). |
PR Summary
Fix #10656.
PR Context
Mads Torgersen rightly pointed out at this point in a .NET Conf 2019 demonstration he delivered that testing for
$null
using-is
is more accurate than testing for$null
using-eq
.PowerShell would benefit from having the same support, such that users can check if an object is or is not null by invoking
$x -is $null
or$x -isnot $null
, orWhere-Object Property -is $null
orWhere-Object Property -isnot $null
, for two reasons:$null
.$null
, including collections. Before this change, if you want to test whether or not a collection is$null
, you have to place the$null
on the left-hand side of the operator, which is not possible inWhere-Object
. With this change in place, you can simply keep$null
on the right-hand side of the-is
and-isnot
operators, regardless of the type of object on the left-hand side that you are comparing to$null
.Here are the new patterns that will be supported once this PR is merged:
Note that
-is $null
will return$true
if the left hand side is$null
,[DBNull]::Value
,[NullString]::Value
, or[System.Management.Automation.Internal.AutomationNull]::Value
.Also note that this only works when you use a null literal on the right-hand side of
-is
and-isnot
, regardless of whether you use the operators orWhere-Object
. The explicit check for a null literal is necessary to prevent the use of other variables or subexpressions on the right-hand side.PR Checklist
.h
,.cpp
,.cs
,.ps1
and.psm1
files have the correct copyright headerWIP:
or[ WIP ]
to the beginning of the title (theWIP
bot will keep its status check atPending
while the prefix is present) and remove the prefix when the PR is ready.$null
using-is
and-isnot
in Where-Object parameter documentation as well as about_Comparison_operators MicrosoftDocs/PowerShell-Docs#4890