Skip to content

Add use static <class> usage to allow static member access in PowerShell #5099

Open

Description

Reference: #3603

Proposal is to add usage to the using statement to allow for ease of access to static members of classes.

one possible syntax:

using static System.Math

$b  = Pow(1024, 2)

This issue is open to discuss possible implementations, issues concerns, use cases, yays and nays.

Questions: Methods only? All members? syntax? support for PowerShell v5+ user defined classes? precedence?

Activity

  1. added
    WG-Language
    Issue-Discussion
    on Oct 12, 2017
  2. lzybkr commented on Oct 12, 2017

    @lzybkr
    Contributor

    The proposed syntax is ambiguous with commands.

    Today, method invocation uses an operator with an expression before the method name (e.g. $o. or [t]:: ), otherwise the parser sees a command invocation.

    The parentheses do not help disambiguate, and there are other quirks to note:

    echo(1)     # Valid
    echo(1, 2)  # Parse error if strict mode enabled
    echo (1, 2) # Valid
    echo()      # Parse error, missing expression

    The strict mode error only happens if there is no space, as folks learning PowerShell were invoking functions like methods, but surprised that arguments weren't passed as expected. If there is a single argument, the parens don't matter, but if there are multiple arguments, the parens group the values in the first argument.

    Some possible solutions for the ambiguity:

    • Call static methods like functions:
    using static Math
    using static Console
    pow 1 2
    pow -y 2 -x 1 # Same as previous line, named args
    WriteLine "hello"
    • Continue to use :: (a breaking change, but probably safe):
    using static Math
    ::pow(1,2)
    • Extra work at parse time (before semantic checks) so method/property names are not parsed like commands.
    using static Math
    $e = E # [Math]::E
    log(2, $e)
  3. markekraus commented on Oct 12, 2017

    @markekraus
    ContributorAuthor

    personally, I like the idea of them becoming function-esque. But if property members are also exposed, that might look weird.

    using static System.Globalization.CultureInfo 
    
    CurrentCulture = CreateSpecificCulture "nl-NL"

    That looks odd.

    I like the :: prefix/sigil idea. Makes it a little less confusing. i was thinking a sigil would be nice, and there already kind of is one for this usage.

  4. added
    Issue-Enhancement
    on Oct 17, 2017
  5. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  6. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  7. added
    Resolution-No Activity
    on Nov 16, 2023
  8. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  9. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

  10. added
    KeepOpen
    and removed
    Resolution-No Activity
    on Nov 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-DiscussionIssue-EnhancementKeepOpenWG-Language

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions