Skip to content

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

@0xfeeddeadbeef

Description

@0xfeeddeadbeef

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifIssue-Enhancementthe issue is more of a feature request than a bugResolution-FixedThe issue is fixed.WG-Languageparser, language semantics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions