Title: Make meaningless [pscustomobject] casts report an error · Issue #13836 · PowerShell/PowerShell · GitHub
Open Graph Title: Make meaningless [pscustomobject] casts report an error · Issue #13836 · PowerShell/PowerShell
X Title: Make meaningless [pscustomobject] casts report an error · Issue #13836 · PowerShell/PowerShell
Description: Summary of the new feature/enhancement As a user, I want to be notified when a cast doesn't work as intended: # OK - a statement-terminating error occurs if the operand cannot be cast to [int] PS> [int] 'Not a number.' InvalidArgument: C...
Open Graph Description: Summary of the new feature/enhancement As a user, I want to be notified when a cast doesn't work as intended: # OK - a statement-terminating error occurs if the operand cannot be cast to [int] PS> ...
X Description: Summary of the new feature/enhancement As a user, I want to be notified when a cast doesn't work as intended: # OK - a statement-terminating error occurs if the operand cannot be cast to [int] ...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/13836
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Make meaningless [pscustomobject] casts report an error","articleBody":"## Summary of the new feature/enhancement\r\n\r\nAs a user, I want to be notified when a cast doesn't work as intended:\r\n\r\n```powershell\r\n# OK - a statement-terminating error occurs if the operand cannot be cast to [int]\r\nPS> [int] 'Not a number.'\r\nInvalidArgument: Cannot convert value \"Not a number.\" to type \"System.Int32\". Error: \"Input string was not in a correct format.\"\r\n```\r\n\r\nHowever, this doesn't currently work as intended with a `[pscustomobject]` cast:\r\n\r\n```powershell\r\n# A meaningless cast:\r\n# !! Seemingly quietly ignored - in reality, the operand is simply wrapped in [psobject](!)\r\nPS> [pscustomobject] 42\r\n42\r\n```\r\n\r\nUnexpectedly, the above is equivalent to `[psobject] 42` and wraps the operand in a large largely invisible `[psobject]` instance (except it isn't _always_ invisible - see #5579).\r\nWhile the utility of such a cast with `[psobject]` is questionably in itself, with `[pscustomobject]` it is downright puzzling (and even though _both_ type accelerators refer to `System.Management.Automation.PSObject`, PowerShell can and does treat them differently with a hashtable operand (see below)).\r\n\r\nThe only meaningful use of a `[pscustomobject]` cast is (currently) the _syntactic sugar_ supported with a single (ordered) `[hashtable]` operand, to convert a hash table to a custom object; e.g., `[pscustomobject] @{ one = 1; two = 2 }` (Sadly, it doesn't work with a generic ``Dictionary`2`` instance, though #13727 may change that; it also doesn't currently work as an _array_ cast - see #13838).\r\n\r\nAll other uses should report a statement-terminating error.\r\n\r\nNote: While this is technically a breaking change, existing instances of such casts amount to already broken code.\r\n\r\n","author":{"url":"https://github.com/mklement0","@type":"Person","name":"mklement0"},"datePublished":"2020-10-22T19:53:42Z","url":"https://github.com/PowerShell/PowerShell/issues/13836"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| 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:93a23de0-4bf6-3d54-bfe8-6a76c8d58128 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C9D0:27C34E:2F701C:42B204:6AFDB3EE |
| html-safe-nonce | 4afac5f8002bbe7345c66a69ab7a423fe22d921aaf31fd519ed9b3d57c877152 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDOUQwOjI3QzM0RToyRjcwMUM6NDJCMjA0OjZBRkRCM0VFIiwidmlzaXRvcl9pZCI6IjE2Njk1NTUwMDA2NTQ1NDE4MDYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | b98ebfe16c5b0a1d43022bd11be7a1b4bab5a2a9eeb98681e91b4b4c00254ef4 |
| hovercard-subject-tag | issue:727678546 |
| 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/13836/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a8c6cc9147e6c776c6a6567766d61edf6fe1963bd718051b021b01c84f16db24/PowerShell/PowerShell/issues/13836 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a8c6cc9147e6c776c6a6567766d61edf6fe1963bd718051b021b01c84f16db24/PowerShell/PowerShell/issues/13836 |
| og:image:alt | Summary of the new feature/enhancement As a user, I want to be notified when a cast doesn't work as intended: # OK - a statement-terminating error occurs if the operand cannot be cast to [int] PS> ... |
| 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 | 8309f07eb60a4237a657068c83d0bb57be4c7e02937f5e9933126f4bd596aac4 |
| 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 | 2f7154d847f1ddc5baa83b109a3e1d924605e2a3 |
| ui-target | canary-1 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width