Title: Suggestion: implement null-coalescing, null-conditional access (null-soaking), null-conditional assignment · Issue #3240 · PowerShell/PowerShell · GitHub
Open Graph Title: Suggestion: implement null-coalescing, null-conditional access (null-soaking), null-conditional assignment · Issue #3240 · PowerShell/PowerShell
X Title: Suggestion: implement null-coalescing, null-conditional access (null-soaking), null-conditional assignment · Issue #3240 · PowerShell/PowerShell
Description: Null-coalescing and null-conditonal access (null-soaking) would be handy additions to the language. Update: @BrucePay additionally suggests null-conditional assignment, with ?= - see comment below. For instance, instead of writing: if ($...
Open Graph Description: Null-coalescing and null-conditonal access (null-soaking) would be handy additions to the language. Update: @BrucePay additionally suggests null-conditional assignment, with ?= - see comment below....
X Description: Null-coalescing and null-conditonal access (null-soaking) would be handy additions to the language. Update: @BrucePay additionally suggests null-conditional assignment, with ?= - see comment below....
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/3240
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Suggestion: implement null-coalescing, null-conditional access (null-soaking), null-conditional assignment","articleBody":"**Null-coalescing** and **null-conditonal access** (null-soaking) would be handy additions to the language.\r\n\r\n_Update_: @BrucePay additionally suggests **null-conditional _assignment_**, with `?=` - see [comment below](https://github.com/PowerShell/PowerShell/issues/3240#issuecomment-417435164).\r\n\r\nFor instance, instead of writing:\r\n\r\n```powershell\r\nif ($null -ne $varThatMayBeNull) { $varThatMayBeNull } else { $fallbackValue }\r\n#\r\nif ($null -ne $varThatMayBeNull) { $varThatMayBeNull.Name } else { $null }\r\n```\r\n\r\none might be able to write:\r\n\r\n```powershell\r\n$varThatMayBeNull ?? $fallbackValue # null-coalescing\r\n# \r\n$varThatMayBeNull?.Name # null-conditional access, for Set-StrictMode -Version 2+\r\n$varThatMayBeNull?[42] # ditto, with indexing\r\n```\r\n\r\nRe null-conditional access: With `Set-StrictMode` being OFF (the default), you can just use `$varThatMayBeNull.Name` - no special syntax needed; however, if `Set-StrictMode -Version 2` or higher is in effect, `$varThatMayBeNull.Name` would _break_ and that's where the null-conditional operator (`?.`) is helpful, to signal the explicit intent to ignore the `$null` in a concise manner.\r\n\r\n---\r\n\r\n**Open question**:\r\n\r\n`$varThatMayBeNull?[42]` handles the case where the variable is `$null`, but if it isn't, an array element with the specified index _must exist_.\r\n\r\nIt would therefore also be helpful to **make _indexing_ null-conditional** - something that C# does _not_ support, incidentally (you have to use `.ElementAtOrDefault()`).\r\n\r\nThe two basic choices are:\r\n\r\n* Come up with additional syntax that explicitly signal the intent to ignore a non-existent _index_:\r\n\r\n * The question is what syntax to choose for this, given that `?[...]` and `[...]?` are not an option due to ambiguity. \r\n * Perhaps `[...?]`, but that seems awkward .\r\n\r\n* Rely on the existing behavior with respect to accessing non-existent indices, as implied by the `Set-StrictMode` setting - see [table below](https://github.com/PowerShell/PowerShell/issues/3240#issuecomment-478599343).\r\n\r\n---\r\n\r\nRelated: [implement ternary conditionals](https://github.com/PowerShell/PowerShell/issues/3239)","author":{"url":"https://github.com/mklement0","@type":"Person","name":"mklement0"},"datePublished":"2017-03-02T20:28:43.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":71},"url":"https://github.com/3240/PowerShell/issues/3240"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:d4e64b1f-294c-4aac-dd3d-4d48f6f51060 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CB3C:1F8EF7:5DC862:7E7034:6A5A7B77 |
| html-safe-nonce | 59a62ee94b90477c7c6ec4c3081acad3c67dc3b3be2ddae4c30cb13f42a08fed |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQjNDOjFGOEVGNzo1REM4NjI6N0U3MDM0OjZBNUE3Qjc3IiwidmlzaXRvcl9pZCI6IjkxNTcyMDA1MTI1OTY3MzY4ODciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | d1900c2bb6e02464a2a4aa10e01c2b66b2ee40da2b6135ce8330b03e2881adb5 |
| hovercard-subject-tag | issue:211507831 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/3240/issue_layout |
| twitter:image | https://opengraph.githubassets.com/983740e59b32a536a98b130ff32748e54a1c1f44cd72e68c369c3beb9f5dac01/PowerShell/PowerShell/issues/3240 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/983740e59b32a536a98b130ff32748e54a1c1f44cd72e68c369c3beb9f5dac01/PowerShell/PowerShell/issues/3240 |
| og:image:alt | Null-coalescing and null-conditonal access (null-soaking) would be handy additions to the language. Update: @BrucePay additionally suggests null-conditional assignment, with ?= - see comment below.... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mklement0 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 922bdba685ea48400234d557977f0e32d9adbae6f5125725b4f48cacc6801106 |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 0437efb828a4041956ce45d1f7fe69fff38926c7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width