Title: Enhance hash table syntax · Issue #13817 · PowerShell/PowerShell · GitHub
Open Graph Title: Enhance hash table syntax · Issue #13817 · PowerShell/PowerShell
X Title: Enhance hash table syntax · Issue #13817 · PowerShell/PowerShell
Description: Runtime Construct and Populate At design-time the syntax of an array and a dictionary (hash table) may look quiet consistent: $Array = @(1, 4, 9) # or just $a = 1, 4, 9 $Hashtable = @{1 = 1; 2 = 3; 3 = 9} But at runtime there is quiet a ...
Open Graph Description: Runtime Construct and Populate At design-time the syntax of an array and a dictionary (hash table) may look quiet consistent: $Array = @(1, 4, 9) # or just $a = 1, 4, 9 $Hashtable = @{1 = 1; 2 = 3;...
X Description: Runtime Construct and Populate At design-time the syntax of an array and a dictionary (hash table) may look quiet consistent: $Array = @(1, 4, 9) # or just $a = 1, 4, 9 $Hashtable = @{1 = 1; 2 = 3;...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/13817
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Enhance hash table syntax","articleBody":"### Runtime Construct and Populate\r\nAt design-time the syntax of an array and a dictionary (hash table) may look quiet consistent:\r\n```PowerShell\r\n$Array = @(1, 4, 9) # or just $a = 1, 4, 9\r\n$Hashtable = @{1 = 1; 2 = 3; 3 = 9}\r\n```\r\nBut at **runtime** there is quiet a difference; it is possible to *construct and populate* a array in ones:\r\n```PowerShell\r\n$Array = @(1..3 | %{ $_ * $_ })\r\n```\r\nbut it is not possible to *construct and populate* a dictionary in ones. AFAIK, I always need to construct it first and then populate it:\r\n```PowerShell\r\n$Hashtable = @{}\r\n1..3 | %{ $Hashtable[$_] = $_ * $_ }\r\n```\r\nBasically, it is not possible to put a function in a hash table syntax, like: `@{ MyFunction }`, as there is no way to get out of argument mode, in comparison with an array syntax, where it is easy to jump to expression mode `@( MyFunction )`. For the same reason it is not possible to initiate and populate the hash table from within `@{...}` during runtime.\r\n\r\nThe same limitation counts for using a .Net constructor. I can populate an array within an array constructor:\r\n```PowerShell\r\n$Array = [array](1..3 | %{ $_ * $_ })\r\n```\r\nBut it is not possible to something similar from within a hash table constructor:\r\n```PowerShell\r\n$Hashtable = [hashtable](1..3 | %{ @{ $_ = $_ * $_ } })\r\n```\r\nOr to invoke a function like:\r\n```PowerShell\r\n$Hashtable = [hashtable]MyKeyValuePairs\r\n```\r\n> InvalidArgument: Cannot convert the \"System.Object[]\" value of type \"System.Object[]\" to type \"System.Collections.Hashtable\".\r\n\r\n**Purpose**\r\nIt would be nice if the hash table constructor accepts a list KeyValuePair (hash table) objects (`Object[]]`) to better support the [Single-responsibility principle](https://en.wikipedia.org/wiki/Single-responsibility_principle).\r\n\r\n**Caveats** \r\n* A single item (anything but a key value pair, e.g.:`@{ 1 = 'One' }, 2, @{ 3 = 'Three' }`) probably needs to be considered as a key with a `$Null` value.\r\n* Duplicate keys need to be handled, e.g.: overwrite by default and throw an error when `Set-StrictMode` is set or put the related values of the duplicated keys in an array: `[hashtable]@{ 1 = 'One' }, @{2 = 'Two' }, @{ 1 = 'Also One' }` 🡆 `[hashtable]@{ 1 = 'One', 'Also One'; 2 = 'Two' }` \r\n\r\nThe same goes for a `[PSCustomObject]`, it isn't possible to construct and populate a new PSCustomObject in once at runtime. Meaning, to construct a `[PSCustomObject]` and populate it with custom properties at runtime, it is required to construct the `[PSCustomObject]` first, and than populate it, using `Add-Member`. See e.g. StackOverflow question [Is it possible to do an for “for” in an PSObject?](https://stackoverflow.com/a/64385996/1701026). \r\n```PowerShell\r\n$Object = [PSCustomObject](1..3 | %{ @{ \"Name$_\" = $_ * $_ } })\r\n```\r\nThere is no error, but instead, I get a list of hash table objects (`[hashtable[]]`) rather than a single PSCustomObject with 3 properties:\r\n```PowerShell\r\nPS C:\\> $Object.Count\r\n3\r\nPS C:\\> $Object[0].GetType()\r\nIsPublic IsSerial Name BaseType\r\n-------- -------- ---- --------\r\nTrue True Hashtable System.Object\r\n```\r\nThere is probably a good explanation for this but I would expect a single `[PSCustomObject]` with three properties, like:\r\n```PowerShell\r\nName1 Name2 Name3\r\n----- ----- -----\r\n 1 4 9\r\n```\r\nPresuming that for a `[PSCustomObject]` it would mean a breaking change, a `[Hashtable]` constructor as purposed would still allow to do something like this:\r\n```PowerShell\r\n$Object = [PSCustomObject][Hashtable]MyPropertyFunction\r\n```\r\nOr directly:\r\n```PowerShell\r\n$Object = [PSCustomObject][Hashtable](1..3 | %{ @{ \"Name$_\" = $_ * $_ } })\r\n```\r\n\r\nThis purpose is related to #5643. \r\nIn addition to the shortcut purpose from @iSazonov, for the object `@[]` shortcut accelerator, `@{}` could also be an shortcut for the hash table accelerator (wishful thinking?):\r\n```PowerShell\r\n$Hashtable = @{}MyPropertyFunction\r\n$Object = @[]$Hashtable\r\n```\r\n\r\n### Design-time Construct and Populate\r\n\r\nI am not sure whether this part makes sense in a way it is a more a less intuitive/consistent with the current syntax, but I like to mention it anyways as it has at least some consistency with the runtime purpose above: \r\n\r\nTo assign an array, it is possible to just supply a list of items separated with a comma:\r\n```PowerShell\r\n$Array = 1, 2, 3\r\n```\r\nIt would be nice to also be able to do that for creating a list of key-value pairs, e.g.:\r\n```PowerShell\r\n$KeyValues = 1 = 'One', 2 = 'Two', 3 = 'Three' \r\n```\r\nKnowing that this currently this has no meaning and returns an error:\r\n```\r\nLine |\r\n 1 | $KeyValues = 1 = 'One', 2 = 'Two', 3 = 'Three'\r\n | ~\r\n | The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept assignments, such as a variable or a property.\r\n```\r\nBut is could return a list of `[hashtable]` items, similar to:\r\n```PowerShell\r\n$KeyValues = @{ 1 = 'One' }, @{ 2 = 'Two' }, @{ 3 = 'Three' }\r\n```\r\nWhich on its own could be used for simple enumerations (even there is no binary index and requires to enumerate twice):\r\n```PowerShell\r\n$KeyValues.GetEnumerator().GetEnumerator() | Foreach-Object { Write-Host ('{0} = {1}' -f $_.Key, $_.Value) }\r\n```\r\nWhich than could than be casted to a single hash table as suggested in the above **Runtime** section, like:\r\n```PowerShell\r\n$Hashtable = [hashtable](1 = 'One', 2 = 'Two', 3 = 'Three')\r\n```\r\n**Caveats**\r\n* String keys always required to be quoted: `$Hashtable = [hashtable]('One' = 1, 'Two' = 2, 'Three' = 3)`\r\n* Variable keys need to be double quoted: `$Hashtable = [hashtable](1..3 | %{ \"$_\" = $_ * $_ })`, which is probably unsafe.\r\n","author":{"url":"https://github.com/iRon7","@type":"Person","name":"iRon7"},"datePublished":"2020-10-20T13:48:39Z","url":"https://github.com/PowerShell/PowerShell/issues/13817"}
| 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:c0687af6-5486-9579-2ff4-6861ab589627 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D4F6:2A8262:27AB46:37E5A2:6AFDB21D |
| html-safe-nonce | 2d9e08f233eec957b99e54705b95b3d1441d00ae48180022b779508f935f22eb |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENEY2OjJBODI2MjoyN0FCNDY6MzdFNUEyOjZBRkRCMjFEIiwidmlzaXRvcl9pZCI6IjQ5MDgxNjAwNzA3MDI3MDcyMjkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | e622b48c85ff6df70713437d31fbdcb10a974fe74c3f46f80177c6c0172ed6b8 |
| hovercard-subject-tag | issue:725609542 |
| 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/13817/issue_layout |
| twitter:image | https://opengraph.githubassets.com/05c17f968fd81528623f9efa9b76354189099fa60b5f461d6ee3742d891a70e4/PowerShell/PowerShell/issues/13817 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/05c17f968fd81528623f9efa9b76354189099fa60b5f461d6ee3742d891a70e4/PowerShell/PowerShell/issues/13817 |
| og:image:alt | Runtime Construct and Populate At design-time the syntax of an array and a dictionary (hash table) may look quiet consistent: $Array = @(1, 4, 9) # or just $a = 1, 4, 9 $Hashtable = @{1 = 1; 2 = 3;... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | iRon7 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 3c532f4d069d6fbe9b1837450343d1b9e0619a7d5c4ba0b3b64fd8e0bae67c7e |
| 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 | 5396ad00d1c6d19097aee7fc4d00a2eb03432c8f |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width