Title: Surprising behavior of @() (array subexpression operator) with arrays/collections created with New-Object · Issue #4257 · PowerShell/PowerShell · GitHub
Open Graph Title: Surprising behavior of @() (array subexpression operator) with arrays/collections created with New-Object · Issue #4257 · PowerShell/PowerShell
X Title: Surprising behavior of @() (array subexpression operator) with arrays/collections created with New-Object · Issue #4257 · PowerShell/PowerShell
Description: tl; dr: This issue is written based on the following, currently unfulfilled expectation: When you wrap a New-Object call that outputs an array / collection in @(), it should not create an array wrapper around it. @PetSerAl disagrees with...
Open Graph Description: tl; dr: This issue is written based on the following, currently unfulfilled expectation: When you wrap a New-Object call that outputs an array / collection in @(), it should not create an array wra...
X Description: tl; dr: This issue is written based on the following, currently unfulfilled expectation: When you wrap a New-Object call that outputs an array / collection in @(), it should not create an array wra...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/4257
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Surprising behavior of @() (array subexpression operator) with arrays/collections created with New-Object","articleBody":"**tl; dr**:\r\n\r\nThis issue is written based on the following, currently unfulfilled expectation:\r\n\r\n* When you wrap a `New-Object` call that outputs an array / collection in `@()`, it should not create an array wrapper around it.\r\n\r\n@PetSerAl disagrees with my expectation (quotes from the comments on this [SO post](https://stackoverflow.com/a/45091504/45375) that inspired this issue, part of which is reprinted below):\r\n\r\n\u003e What is unexpected about this behavior? `New-Object` writes a single element to the pipeline and `@()` wraps it in an array.\r\n\r\nOn the tangentially related issue that `@()` preserves the specific array type:\r\n\r\n\u003e Also, IMHO, `@([int[]] (1, 2)).GetType().Name` [returning `Int32[]`] is a bug (over-optimization; it returns `Object[]` in v2)\r\n\r\n---\r\n\r\nAs of Windows PowerShell v5.1 / PowerShell Core v6.0.0-beta.4, **`@()` unexpectedly wraps arrays / collections instantiated directly as .NET types with the `New-Object` cmdlet in an outer, single-element array**; in other words: it doesn't recognize that the results already _are_ array-valued:\r\n\r\n```powershell\r\n\u003e @(New-Object 'Object[]' 2).Count; @(New-Object 'Object[]' 2)[0].Count\r\n1 # !! The array was unexpectedly wrapped in an outer single-item array.\r\n2 # !! Element [0] contains the original array.\r\n\r\n\u003e @(New-Object 'System.Collections.ArrayList').Count; @(New-Object 'System.Collections.ArrayList')[0].Count\r\n1 # !! The array list was unexpectedly wrapped in an outer single-item array.\r\n0 # !! Element [0] contains the original (empty) array list.\r\n```\r\n---\r\n\r\nTo contrast the surprising `New-Object` behavior above with commands that _should_ be equivalent, but work as expected:\r\n\r\n```powershell\r\n\u003e @((New-Object 'Object[]' 2)).Count\r\n2 # OK - !! Simply enclosing the New-Object call in (...) made the difference.\r\n\r\n\u003e @([int[]] (1, 2)).Count\r\n2 # OK - using a cast in lieu of New-Object\r\n\r\n\u003e @([System.Collections.ArrayList]::new()).Count\r\n0 # OK - using the static ::new() method in lieu of New-Object\r\n```\r\n\r\n\r\nEnvironment data\r\n----------------\r\n\r\n```powershell\r\nPowerShell Core v6.0.0-beta.4 on macOS 10.12.5\r\nPowerShell Core v6.0.0-beta.4 on Ubuntu 16.04.2 LTS\r\nPowerShell Core v6.0.0-beta.4 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)\r\nWindows PowerShell v5.1.15063.413 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)\r\n```\r\n","author":{"url":"https://github.com/mklement0","@type":"Person","name":"mklement0"},"datePublished":"2017-07-14T19:57:25.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":16},"url":"https://github.com/4257/PowerShell/issues/4257"}
| 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:f5c47929-2f98-232a-7497-f45992d1e6ef |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DE56:3C71F0:B2E6E9:EFAC0F:6A5B92DA |
| html-safe-nonce | b2a22d1c25dbc89ae2e923bf924d10d15c68fa699e3ea4f43b05af543e15c8c8 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERTU2OjNDNzFGMDpCMkU2RTk6RUZBQzBGOjZBNUI5MkRBIiwidmlzaXRvcl9pZCI6IjYwMDU1NzIyMDY3ODQ2NDc4OTgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 54c65d8a6f2eee39a7da39d1bfeebf21ebf4a124365c2e64ffeb08a7be1424a5 |
| hovercard-subject-tag | issue:243096692 |
| 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/4257/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d87d068cccbadc2ad1db96cdc9fdecc5f31a91030923ce8d0aafe67eb7261525/PowerShell/PowerShell/issues/4257 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d87d068cccbadc2ad1db96cdc9fdecc5f31a91030923ce8d0aafe67eb7261525/PowerShell/PowerShell/issues/4257 |
| og:image:alt | tl; dr: This issue is written based on the following, currently unfulfilled expectation: When you wrap a New-Object call that outputs an array / collection in @(), it should not create an array wra... |
| 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 | 5290d7e14309ad1e76106a9c4237bd1041517e83ea182c8ab756752cb0c6940b |
| 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 | 9c975978430e9ad293956f2bbdaf153b1bd84a99 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width