Title: Make -is [pscustomobject] and -as [pscustomobject] work meaningfully · Issue #11921 · PowerShell/PowerShell · GitHub
Open Graph Title: Make -is [pscustomobject] and -as [pscustomobject] work meaningfully · Issue #11921 · PowerShell/PowerShell
X Title: Make -is [pscustomobject] and -as [pscustomobject] work meaningfully · Issue #11921 · PowerShell/PowerShell
Description: Note: The following discusses the issue with respect to the -is operator, but the same applies to the -as operator (which is currently broken even when using the full type name - see #4343). -is [pscustomobject] (and analogously -as [psc...
Open Graph Description: Note: The following discusses the issue with respect to the -is operator, but the same applies to the -as operator (which is currently broken even when using the full type name - see #4343). -is [p...
X Description: Note: The following discusses the issue with respect to the -is operator, but the same applies to the -as operator (which is currently broken even when using the full type name - see #4343). -is [p...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/11921
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Make -is [pscustomobject] and -as [pscustomobject] work meaningfully","articleBody":"Note: The following discusses the issue with respect to the `-is` operator, but the same applies to the \r\n`-as` operator (which is currently broken even when using the _full_ type name - see #4343).\r\n\r\n`-is [pscustomobject]` (and analogously `-as [pscustomobject]`) should work meaningfully as follows:\r\n\r\n```powershell\r\n# -is [pscustomobject] should only be true for true \r\n# [System.Management.Automation.PSCustomObject] instances\r\n# (PSObject instances without a .NET base object)\r\n\r\n[pscustomobject] @{} -is [pscustomobject] | Should -BeTrue # OK\r\n42 -is [pscustomobject] | Should -BeFalse # OK\r\n(Get-Item /) -is [pscustomobject] | Should -BeFalse # BROKEN\r\n(Write-Output 42) -is [pscustomobject] | Should -BeFalse # BROKEN\r\n```\r\n\r\n----\r\n\r\n### Background Information\r\n\r\nCurrently, the only way to test meaningfully for whether a given value is a custom object is to do the use the _full_ type name:\r\n\r\n```powershell\r\n# Works, but only with the *full* type name.\r\n[pscustomobject] @{} -is [System.Management.Automation.PSCustomObject] | Should -BeTrue\r\n(Get-Item /) -is [System.Management.Automation.PSCustomObject] | Should -BeFalse\r\n```\r\n\r\nSurprisingly, `[pscustomobject]` does _not_ work meaningfully:\r\n\r\n```powershell\r\n# !! Returns $true, because [pscustomobject] is really [psobject]\r\n(Get-Item /) -is [pscustomobject] \r\n```\r\n\r\nThe reason is that `[pscustomobject]` is unexpectedly the same as `[psobject]` (!), i.e., it is type accelerator for `System.Management.Automation.PSObject` rather than for `System.Management.Automation.PSCustomObject`, for historical reasons (see #4344). \r\nTherefore, _any_ `[psobject]`-wrapped .NET object returns `$true` for `-is [pscustomobject]`, which is both confusing and virtually useless.\r\n\r\nNote that there already _is_ precedent for _situationally_ distinguishing between `[pscustomobject]` and `[psobject]`, namely in the context of custom-object _literals_ via _syntactic sugar_ `[pscustomobject] @{ ... }` (constructs a custom object), which doesn't work with `[psobject] @{ ... }` (constructs a hashtable and uselessly wraps it in `[psobject]`).\r\n\r\nSee also:\r\n\r\n* #9997, where meaningful use of `-is [AutomationNull]` is proposed to allow distinguishing `[System.Management.Automation.Internal.AutomationNull]::Value` values from true `$null` values.\r\n\r\n* Pending PR #10704, which implements the ability to use`-is $null ` to test for `$null` values.","author":{"url":"https://github.com/mklement0","@type":"Person","name":"mklement0"},"datePublished":"2020-02-21T19:58:48.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":4},"url":"https://github.com/11921/PowerShell/issues/11921"}
| 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:b81f1eab-3a3c-8575-84aa-705bb8781d97 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | ED72:19C3B6:CA61BC:122066F:6A5A02FA |
| html-safe-nonce | 00c284383a519b9ea8f5cedb2d7b15e203e9e806ab3c9fd0ca8a588ed7991ad1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFRDcyOjE5QzNCNjpDQTYxQkM6MTIyMDY2Rjo2QTVBMDJGQSIsInZpc2l0b3JfaWQiOiIzMDkwNTg1MTc4NjYzOTQ0OTU0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | aa9d630fc281fba71c633cf64fc19dd46064300eb44f605cb1fa4243be67fda0 |
| hovercard-subject-tag | issue:569155558 |
| 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/11921/issue_layout |
| twitter:image | https://opengraph.githubassets.com/22742d55e9c54ff0608c2064863be847054d93a6a7d2619cf0ad348b0a5cd60c/PowerShell/PowerShell/issues/11921 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/22742d55e9c54ff0608c2064863be847054d93a6a7d2619cf0ad348b0a5cd60c/PowerShell/PowerShell/issues/11921 |
| og:image:alt | Note: The following discusses the issue with respect to the -is operator, but the same applies to the -as operator (which is currently broken even when using the full type name - see #4343). -is [p... |
| 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 | 4d3f961cfc83a4348ee5be7691cf4ae3515aab3ef5200c13a7e41b048125fb61 |
| 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 | ac4d28603af300ac851b62521abb878ab5d3319a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width