Null member access operators ?. and ?[]#10960
Conversation
src/System.Management.Automation/engine/ExperimentalFeature/ExperimentalFeature.cs
Outdated
Show resolved
Hide resolved
?. and ?[]?. and ?[]
src/System.Management.Automation/engine/ExperimentalFeature/ExperimentalFeature.cs
Outdated
Show resolved
Hide resolved
|
@PoshChan please retry Windows |
|
@adityapatwardhan, successfully started retry of |
There was a problem hiding this comment.
Remove trailing spaces here.
There was a problem hiding this comment.
In C#, there is no point to support ?. for member access to static members, but in PowerShell, you can do $a = [string]; $a::Equals(...), which makes it debatable whether we want to support it.
There was a problem hiding this comment.
Though it is technically possible to implement something like: $a = [string]; ${a}?::Equals(...), i doubt there is any usefulness to this.
There was a problem hiding this comment.
Yeah, it's technically possible. I vote for postponing it until we have a ask. @SteveL-MSFT any thoughts?
There was a problem hiding this comment.
Though it is technically possible to implement something like:
$a = [string]; ${a}?::Equals(...), i doubt there is any usefulness to this.
Where I think it would be most useful is situations where you want to invoke a static method only if an assembly is already loaded. e.g.
('MyCustomType' -as [type])?::Initialize()There was a problem hiding this comment.
That certainly beats my current method! (and has better utility, probably, as well)
switch ($null) {
('typename' -as [type]) { . Type.ps1 }
('type2name' -as [type]) { . Type2.ps1 }
}There was a problem hiding this comment.
Since it's additive we can postpone
src/System.Management.Automation/engine/ExperimentalFeature/ExperimentalFeature.cs
Outdated
Show resolved
Hide resolved
src/System.Management.Automation/engine/ExperimentalFeature/ExperimentalFeature.cs
Outdated
Show resolved
Hide resolved
src/System.Management.Automation/engine/ExperimentalFeature/ExperimentalFeature.cs
Outdated
Show resolved
Hide resolved
src/System.Management.Automation/engine/ExperimentalFeature/ExperimentalFeature.cs
Outdated
Show resolved
Hide resolved
src/System.Management.Automation/engine/ExperimentalFeature/ExperimentalFeature.cs
Outdated
Show resolved
Hide resolved
src/System.Management.Automation/engine/ExperimentalFeature/ExperimentalFeature.cs
Outdated
Show resolved
Hide resolved
src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs
Show resolved
Hide resolved
src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs
Show resolved
Hide resolved
src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs
Show resolved
Hide resolved
|
@daxian-dbw - Please re-review. |
src/System.Management.Automation/engine/parser/SemanticChecks.cs
Outdated
Show resolved
Hide resolved
src/System.Management.Automation/engine/parser/SemanticChecks.cs
Outdated
Show resolved
Hide resolved
|
@rjmholt and @JamesWTruher Can you please review this PR as well? Thanks! |
| @@ -404,6 +404,7 @@ internal List<CompletionResult> GetResultHelper(CompletionContext completionCont | |||
|
|
|||
There was a problem hiding this comment.
@adityapatwardhan, your last commit had 1 failures in PowerShell-CI-windows
Enter-PSHostProcess tests.By Process Id.Can enter using NamedPipeConnectionInfo
Exception calling "Invoke" with "0" argument(s): "The runspace state is not valid for this operation."
at <ScriptBlock>, D:\a\1\s\test\powershell\Modules\Microsoft.PowerShell.Core\Enter-PSHostProcess.Tests.ps1: line 136
136: $ps.AddScript('$pid').Invoke() | Should -Be $pwshId
src/System.Management.Automation/engine/parser/SemanticChecks.cs
Outdated
Show resolved
Hide resolved
| @@ -404,6 +404,7 @@ internal List<CompletionResult> GetResultHelper(CompletionContext completionCont | |||
|
|
|||
There was a problem hiding this comment.
@adityapatwardhan, your last commit had 1 failures in PowerShell-CI-windows
NullConditionalMemberAccess.?. operator tests.Use ?. on a dynamic method name that does not exist
Expected an exception, with FullyQualifiedErrorId 'Argument' to be thrown, but the FullyQualifiedErrorId was 'MethodNotFound'. from D:\a\1\s\test\powershell\Language\Operators\NullConditional.Tests.ps1:343 char:15
+ { (Get-Date '11/11/2019')?.$methodName() } | Should -Thro ?
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
at <ScriptBlock>, D:\a\1\s\test\powershell\Language\Operators\NullConditional.Tests.ps1: line 343
343: { (Get-Date '11/11/2019')?.$methodName() } | Should -Throw -ErrorId 'Argument'| @@ -404,6 +404,7 @@ internal List<CompletionResult> GetResultHelper(CompletionContext completionCont | |||
|
|
|||
There was a problem hiding this comment.
@adityapatwardhan, your last commit had 1 failures in PowerShell-CI-linux
NullConditionalMemberAccess.?. operator tests.Use ?. on a dynamic method name that does not exist
Expected an exception, with FullyQualifiedErrorId 'Argument' to be thrown, but the FullyQualifiedErrorId was 'MethodNotFound'. from /home/vsts/work/1/s/test/powershell/Language/Operators/NullConditional.Tests.ps1:343 char:15
+ { (Get-Date '11/11/2019')?.$methodName() } | Should -Thro ?
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
at <ScriptBlock>, /home/vsts/work/1/s/test/powershell/Language/Operators/NullConditional.Tests.ps1: line 343
343: { (Get-Date '11/11/2019')?.$methodName() } | Should -Throw -ErrorId 'Argument'| @@ -404,6 +404,7 @@ internal List<CompletionResult> GetResultHelper(CompletionContext completionCont | |||
|
|
|||
There was a problem hiding this comment.
@adityapatwardhan, your last commit had 1 failures in PowerShell-CI-macos
NullConditionalMemberAccess.?. operator tests.Use ?. on a dynamic method name that does not exist
Expected an exception, with FullyQualifiedErrorId 'Argument' to be thrown, but the FullyQualifiedErrorId was 'MethodNotFound'. from /Users/runner/runners/2.160.0/work/1/s/test/powershell/Language/Operators/NullConditional.Tests.ps1:343 char:15
+ { (Get-Date '11/11/2019')?.$methodName() } | Should -Thro ?
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
at <ScriptBlock>, /Users/runner/runners/2.160.0/work/1/s/test/powershell/Language/Operators/NullConditional.Tests.ps1: line 343
343: { (Get-Date '11/11/2019')?.$methodName() } | Should -Throw -ErrorId 'Argument'| @@ -404,6 +404,7 @@ internal List<CompletionResult> GetResultHelper(CompletionContext completionCont | |||
|
|
|||
There was a problem hiding this comment.
@adityapatwardhan, your last commit had 20 failures in PowerShell-CI-static-analysis
(These are 5 of the failures)
Verify Markdown Links.Verify links in /home/vsts/work/1/s/README.md.https://docs.microsoft.com/powershell/scripting/setup/installing-powershell-core-on-windows?view=powershell-6 should work
retry of URL failed with error: Response status code does not indicate success: 404 (Not Found).
at <ScriptBlock>, /home/vsts/work/1/s/test/common/markdown/markdown-link.tests.ps1: line 117
117: throw "retry of URL failed with error: $($_.Exception.Message)"
Verify Markdown Links.Verify links in /home/vsts/work/1/s/README.md.https://docs.microsoft.com/powershell/scripting/setup/installing-powershell-core-on-linux?view=powershell-6#ubuntu-1804 should work
retry of URL failed with error: Response status code does not indicate success: 404 (Not Found).
at <ScriptBlock>, /home/vsts/work/1/s/test/common/markdown/markdown-link.tests.ps1: line 117
117: throw "retry of URL failed with error: $($_.Exception.Message)"
Verify Markdown Links.Verify links in /home/vsts/work/1/s/README.md.https://docs.microsoft.com/powershell/scripting/setup/installing-powershell-core-on-linux?view=powershell-6#ubuntu-1604 should work
retry of URL failed with error: Response status code does not indicate success: 404 (Not Found).
at <ScriptBlock>, /home/vsts/work/1/s/test/common/markdown/markdown-link.tests.ps1: line 117
117: throw "retry of URL failed with error: $($_.Exception.Message)"
Verify Markdown Links.Verify links in /home/vsts/work/1/s/README.md.https://docs.microsoft.com/powershell/scripting/setup/installing-powershell-core-on-linux?view=powershell-6#debian-9 should work
retry of URL failed with error: Response status code does not indicate success: 404 (Not Found).
at <ScriptBlock>, /home/vsts/work/1/s/test/common/markdown/markdown-link.tests.ps1: line 117
117: throw "retry of URL failed with error: $($_.Exception.Message)"
Verify Markdown Links.Verify links in /home/vsts/work/1/s/README.md.https://docs.microsoft.com/powershell/scripting/setup/installing-powershell-core-on-linux?view=powershell-6#centos-7 should work
retry of URL failed with error: Response status code does not indicate success: 404 (Not Found).
at <ScriptBlock>, /home/vsts/work/1/s/test/common/markdown/markdown-link.tests.ps1: line 117
117: throw "retry of URL failed with error: $($_.Exception.Message)"
There was a problem hiding this comment.
One open issue: [System.DBNull]::Value?.GetTypeCode() and [NullString]::Value?.GetType() will return nothing, which might raise confusion. We need to discuss and decide if it's fine to have this semantics for the null-conditional operator.
Talked with @adityapatwardhan offline, and we will discuss and address this in the RC release.
/cc @JamesWTruher @SteveL-MSFT @rjmholt
|
Filed issue: #11084 |
|
@PoshChan please retry static |
|
@adityapatwardhan, successfully started retry of |
|
@daxian-dbw Ready to merge? |
|
🎉 Handy links: |
PR Summary
PR for
?.and?[]operators.Braces need around variable name. Like
${x}?.Nameand${x}?.Get()Implementation on RFC - PowerShell/PowerShell-RFC#223
PR Context
The null conditional member access operators allow to check the item for being null before a property is accessed or an index is used or a method is invoked. This helps reduced the null checking logic from scripts.
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:or[ WIP ]to the beginning of the title (theWIPbot will keep its status check atPendingwhile the prefix is present) and remove the prefix when the PR is ready.????=?.and?[]operators MicrosoftDocs/PowerShell-Docs#4925????=?.?[]operators EditorSyntax#185