Enhancement: Ability to invoke generic methods with explicit type parameters when PowerShell cannot figure out <T> from the context
See original GitHub issuePlease 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]()
(Array.Empty
was chosen as a simplest possible example. In scripts Iʼd use @()
)
Current behavior
At line:1 char:16
+ [Array]::Empty[string]()
+ ~
Array index expression is missing or not valid.
At line:1 char:16
+ [Array]::Empty[string]()
+ ~~~~~~~
Unexpected token 'string]' in expression or statement.
At line:1 char:24
+ [Array]::Empty[string]()
+ ~
An expression was expected after '('.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingArrayIndexExpression
Ruminations
I 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 🤔)
Here are some not-very-elegant workarounds from the Interwebs:
- Workaround to call to a generic method OfType<T>() in PowerShell, when PowerShell cannot figure out <T> from the context by @vors
- PSGenericMethods module by @dlwyatt
- Invoking Generic Methods on Non-Generic Classes in PowerShell by @LeeHolmes
- How do I call a parameterless generic method from Powershell v3?
Here are my questions:
- Is it possible to implement this at all? How hard it could be?
- Anyone tried to implement this before?
- Is it worthwhile?
Just posting this here for discussion. Didnʼt find any mentions of this problem in other issues.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:25
- Comments:33 (23 by maintainers)
Top Results From Across the Web
about Calling Generic Methods - PowerShell
For some generic methods, PowerShell is able to figure out generic arguments for a method by inferring from the provided arguments.
Read more >How to call a generic method in PowerShell?
In PowerShell 7.2- (including in Windows PowerShell), direct calls to generic methods are only possible if the type argument(s) can be inferred ...
Read more >Argument Completion Attributes - powershell.one
Enable argument completion for your own function parameters and make them so much easier to use! Some lesser-known attributes can help you.
Read more >High Performance PowerShell with LINQ - Simple Talk
As it turns out, calling a generic, static, extension LINQ method requires a rather convoluted incantation in PowerShell.
Read more >Configure | Citrix Workspace app for Windows
Under the Computer Configuration node, go to Administrative Templates > Citrix Components > Citrix Workspace > Workspace Updates. Select the ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
At first glance, this is not worthwhile because the main asset of PowerShell is to hide complex details - Powershell automatically calls the correct methods.
As for implementation it seems is a lot of work but not so difficult.
@iSazonov Problem is that PowerShell can not automatically call correct methods when there is no way to deduce generic type parameter automatically (as seen in “Steps to demonstrate”). It must be specified explicitly. This is true for other CLR languages too. We are forced to dig through all the complex details using reflection (see list of workarounds).