FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Null member access operators `?.` and `?[]` by adityapatwardhan · Pull Request #10960 · PowerShell/PowerShell · GitHub

Null member access operators ?. and ?[] - #10960

Merged
Dongbo Wang (daxian-dbw) merged 26 commits into
PowerShell:masterfrom
adityapatwardhan:NullMemberAccessOperators
Nov 16, 2019
Merged

Null member access operators ?. and ?[]#10960
Dongbo Wang (daxian-dbw) merged 26 commits into
PowerShell:masterfrom
adityapatwardhan:NullMemberAccessOperators

Conversation

Aditya Patwardhan (adityapatwardhan) commented Nov 1, 2019
edited
Loading

Copy link
Copy Markdown
Member

PR Summary

PR for ?. and ?[] operators.
Braces need around variable name. Like ${x}?.Name and ${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

Aditya Patwardhan (adityapatwardhan) changed the title WIP: Null member access operators ?. and ?[] Null member access operators ?. and ?[] Nov 12, 2019
Aditya Patwardhan (adityapatwardhan) marked this pull request as ready for review November 12, 2019 00:48

Copy link
Copy Markdown
Member Author

PoshChan-Bot (@PoshChan) please retry Windows

Copy link
Copy Markdown
Collaborator

Aditya Patwardhan (@adityapatwardhan), successfully started retry of PowerShell-CI-Windows

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Remove trailing spaces here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Fixed

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Not fixed yet.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Though it is technically possible to implement something like: $a = [string]; ${a}?::Equals(...), i doubt there is any usefulness to this.

Dongbo Wang (daxian-dbw) Nov 13, 2019
edited
Loading

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Yeah, it's technically possible. I vote for postponing it until we have a ask. Steve Lee (@SteveL-MSFT) any thoughts?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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()

Rain Sallow (/u/ta11ow) (vexx32) Nov 14, 2019
edited
Loading

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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 }
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Since it's additive we can postpone

Copy link
Copy Markdown
Member Author

Dongbo Wang (@daxian-dbw) - Please re-review.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Not fixed yet.

Copy link
Copy Markdown
Member

Rob Holt (@rjmholt) and James Truher (@JamesWTruher) Can you please review this PR as well? Thanks!

@@ -404,6 +404,7 @@ internal List<CompletionResult> GetResultHelper(CompletionContext completionCont

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Aditya Patwardhan (@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

@@ -404,6 +404,7 @@ internal List<CompletionResult> GetResultHelper(CompletionContext completionCont

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Aditya Patwardhan (@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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Aditya Patwardhan (@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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Aditya Patwardhan (@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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Aditya Patwardhan (@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)"

Dongbo Wang (daxian-dbw) left a comment
edited
Loading

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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 Aditya Patwardhan (@adityapatwardhan) offline, and we will discuss and address this in the RC release.
/cc James Truher (@JamesWTruher) Steve Lee (@SteveL-MSFT) Rob Holt (@rjmholt)

Copy link
Copy Markdown
Member Author

Filed issue: #11084

Copy link
Copy Markdown
Member Author

PoshChan-Bot (@PoshChan) please retry static

Copy link
Copy Markdown
Collaborator

Aditya Patwardhan (@adityapatwardhan), successfully started retry of PowerShell-CI-static-analysis

Copy link
Copy Markdown
Member Author

Dongbo Wang (@daxian-dbw) Ready to merge?

Dongbo Wang (daxian-dbw) merged commit 2579c00 into PowerShell:master Nov 16, 2019
Steve Lee (SteveL-MSFT) added the CL-Experimental Indicates that a PR should be marked as an Experimental Feature in the Change Log label Nov 18, 2019

Copy link
Copy Markdown

🎉v7.0.0-preview.6 has been released which incorporates this pull request.:tada:

Handy links:

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-Experimental Indicates that a PR should be marked as an Experimental Feature in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants


Back | FazBrowse Home | New Git URL