Title: Enhancement: Ability to invoke generic methods with explicit type parameters when PowerShell cannot figure out
Open Graph Title: Enhancement: Ability to invoke generic methods with explicit type parameters when PowerShell cannot figure out
X Title: Enhancement: Ability to invoke generic methods with explicit type parameters when PowerShell cannot figure out
Description: Please provide ability to invoke generic methods with explicit type parameters. Steps to demonstrate [Array]::Empty[string]() or using a class: class A { static [string] GetTypeName[T]() { return [T].Name } } [A]::GetTypeName[string]() (...
Open Graph Description: Please provide ability to invoke generic methods with explicit type parameters. Steps to demonstrate [Array]::Empty[string]() or using a class: class A { static [string] GetTypeName[T]() { return [...
X Description: Please provide ability to invoke generic methods with explicit type parameters. Steps to demonstrate [Array]::Empty[string]() or using a class: class A { static [string] GetTypeName[T]() { return [...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/5146
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Enhancement: Ability to invoke generic methods with explicit type parameters when PowerShell cannot figure out \u003cT\u003e from the context","articleBody":"Please provide ability to invoke generic methods with explicit type parameters.\r\n\r\nSteps to demonstrate\r\n------------------\r\n\r\n```powershell\r\n[Array]::Empty[string]()\r\n```\r\n\r\nor using a class:\r\n\r\n```powershell\r\nclass A {\r\n static [string] GetTypeName[T]() { return [T].Name }\r\n}\r\n\r\n[A]::GetTypeName[string]()\r\n```\r\n\r\n(`Array.Empty` was chosen as a simplest possible example. In scripts Iʼd use `@()`)\r\n\r\nCurrent behavior\r\n---------------\r\n\r\n```none\r\nAt line:1 char:16\r\n+ [Array]::Empty[string]()\r\n+ ~\r\nArray index expression is missing or not valid.\r\nAt line:1 char:16\r\n+ [Array]::Empty[string]()\r\n+ ~~~~~~~\r\nUnexpected token 'string]' in expression or statement.\r\nAt line:1 char:24\r\n+ [Array]::Empty[string]()\r\n+ ~\r\nAn expression was expected after '('.\r\n + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException\r\n + FullyQualifiedErrorId : MissingArrayIndexExpression\r\n```\r\n\r\nRuminations\r\n---------------\r\n\r\nI understand that itʼs not important to have full support of generics in a scripting language or to have 100% feature parity with C# (I might be wrong, — now we have `classes` in PowerShell after all :thinking:)\r\n\r\nHere are some _not-very-elegant_ workarounds from the Interwebs:\r\n\r\n- [Workaround to call to a generic method OfType\u003cT\u003e() in PowerShell, when PowerShell cannot figure out \u003cT\u003e from the context](https://gist.github.com/vors/d6a70e5d3439e928e603) by @vors\r\n- [PSGenericMethods module](https://github.com/dlwyatt/PSGenericMethods/blob/master/GenericMethods.psm1) by @dlwyatt\r\n- [Invoking Generic Methods on Non-Generic Classes in PowerShell](http://www.leeholmes.com/blog/2007/06/19/invoking-generic-methods-on-non-generic-classes-in-powershell/) by @LeeHolmes\r\n- [How do I call a parameterless generic method from Powershell v3?](https://stackoverflow.com/a/18775448)\r\n\r\nHere are my questions:\r\n\r\n- **Is it possible to implement this at all? How hard it could be?**\r\n- **Anyone tried to implement this before?**\r\n- **Is it worthwhile?**\r\n\r\nJust posting this here for discussion. Didnʼt find any mentions of this problem in other issues.","author":{"url":"https://github.com/0xfeeddeadbeef","@type":"Person","name":"0xfeeddeadbeef"},"datePublished":"2017-10-17T19:01:47.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":33},"url":"https://github.com/5146/PowerShell/issues/5146"}
| 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:0089b0e4-8c57-e591-c94e-3521ae1c4f5e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D9FE:2CA0A2:9E1134:D84D3B:6A55AF43 |
| html-safe-nonce | a1ad5f996c77d07c1085bc3d0414f9e21fda8a18ecbfaf679af07c713b549357 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEOUZFOjJDQTBBMjo5RTExMzQ6RDg0RDNCOjZBNTVBRjQzIiwidmlzaXRvcl9pZCI6IjUyNjE0MTQ4MzAwNzA2NzMyMTkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 11b7f358c7ca9bd8e627b1731d08d6416da131455d1c94e277c0ddfc77a4b387 |
| hovercard-subject-tag | issue:266241146 |
| 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/5146/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f772e3b57ffb04367265de88232bdd899a1fa491de639cfc2d4fd0d5a7535ac1/PowerShell/PowerShell/issues/5146 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f772e3b57ffb04367265de88232bdd899a1fa491de639cfc2d4fd0d5a7535ac1/PowerShell/PowerShell/issues/5146 |
| og:image:alt | Please provide ability to invoke generic methods with explicit type parameters. Steps to demonstrate [Array]::Empty[string]() or using a class: class A { static [string] GetTypeName[T]() { return [... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | 0xfeeddeadbeef |
| hostname | github.com |
| expected-hostname | github.com |
| None | b5665c84831ed9ac4fb79519c16c9c5580ba8092fb8bb6e3e72972ec7197348e |
| 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 | 653fccf1e27344851c0d3de3fa6faa526197f072 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width