question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Enhancement: Ability to invoke generic methods with explicit type parameters when PowerShell cannot figure out <T> from the context

See original GitHub issue

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]()

(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:

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:closed
  • Created 6 years ago
  • Reactions:25
  • Comments:33 (23 by maintainers)

github_iconTop GitHub Comments

8reactions
iSazonovcommented, Oct 20, 2017

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.

5reactions
0xfeeddeadbeefcommented, Oct 20, 2017

@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).

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found