Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplified syntax doesn't recognize static members. #21514

Closed
5 tasks done
mklement0 opened this issue Apr 22, 2024 · 9 comments
Closed
5 tasks done

Simplified syntax doesn't recognize static members. #21514

mklement0 opened this issue Apr 22, 2024 · 9 comments
Labels
Issue-Question ideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a WG-Engine core PowerShell engine, interpreter, and runtime

Comments

@mklement0
Copy link
Contributor

mklement0 commented Apr 22, 2024

Prerequisites

Steps to reproduce

Note:

(Add-Type @'
public class Foo
{
  public static string Bar(string s) { return s; }
}
'@ -PassThru)::new() | ForEach-Object Bar '1'

Expected behavior

1

That is, the static .Bar() method should be called.

Actual behavior

The following error occurs:

Input name "Bar" cannot be resolved to a method.

Error details

No response

Environment data

PowerShell 7.5.0-preview.2

Visuals

No response

@mklement0 mklement0 added the Needs-Triage The issue is new and needs to be triaged by a work group. label Apr 22, 2024
@mklement0 mklement0 closed this as not planned Won't fix, can't repro, duplicate, stale Apr 22, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot removed the Needs-Triage The issue is new and needs to be triaged by a work group. label Apr 22, 2024
@mklement0 mklement0 reopened this Apr 22, 2024
@jborean93
Copy link
Collaborator

jborean93 commented Apr 22, 2024

While I agree the documentation in the about page and for the -MemberName parameter in ForEach-Object could be expanded to state this won't work for static methods it does look to be by design. You can't call static methods as a normal member and Get-Member doesn't even show it unless you explicitly opt into to showing static members of that type with Get-Member -Static.

Add-Type @'
public class Foo
{
  public static string Bar(string s) { return s; }
}
'@

$f = [Foo]::new()
$f.Bar('a')  # Doesn't work
$f | Get-Member # Doesn't show Bar

$f | Get-Member -Static  # Does show Bar

@mklement0
Copy link
Contributor Author

@jborean93:

  • It's true that when it comes to direct method calls there's a necessary distinction - driven by syntax requirements - between instance member access (.) and static member access (::), and it is true that this distinction is also reflected in the logic of Get-Member, which reports only instance members by default, and requires -Static to alternatively report static members.

  • By contrast, with simplified syntax there is no requirement to syntactically distinguish between accessing instance and
    static members.

Therefore, there are two possible resolutions:

  • Preferably (to me): Extend simplified syntax to also support accessing static members.

  • Officially declare the current behavior to be by design, and amend the docs accordingly.

@jborean93
Copy link
Collaborator

jborean93 commented Apr 23, 2024

Personally it makes sense for ForEach-Object to run on multiple instances in an array but not for a static method. Why would you be enumerating multiple instances to call a static method that has no correlation to that instance itself. While I don't know what the implementation of this is it sounds like it could add more overhead as now it needs to query both the ETS members and then the members on the type itself which isn't part of the object but the .NET type itself.

Ultimately a question for the WG.

@mklement0 mklement0 changed the title Simplified syntax doesn't recognize static methods. Simplified syntax doesn't recognize static members. Apr 23, 2024
@mklement0
Copy link
Contributor Author

mklement0 commented Apr 23, 2024

You're right:

I've been trying to come with real-world examples of when static member access may be useful with simplified syntax, and so far the only - not very compelling - examples I've come up with are the following:

# Possibly should be the same as:
#   [regex] 'foo' | % { $_.GetType()::CacheSize }
[regex] 'foo' | % CacheSize

# Possibly should be the same as:
#   [regex] 'foo' | % { $_.GetType()::unescape('a\\b') }
[regex] 'foo' | % Unescape 'a\\b'

To put it differently: the suggested ability to access static members would only be useful for static properties and for static methods whose parameters are (by definition) unrelated to the specific properties of each input instance.

Therefore - unless someone finds additional angles - I agree that amending the docs is sufficient.

@rhubarb-geek-nz
Copy link

While not using the simplified syntax it could be done with with reflection.

(Add-Type @'
public class Foo
{
  public static string Bar(string s) { return s; }
}
'@ -PassThru)::new() | ForEach-Object { $_.GetType().GetMethod('Bar',[System.Reflection.BindingFlags]'Static, Public').Invoke($null,[string[]](,'1')) }

@mklement0
Copy link
Contributor Author

@rhubarb-geek-nz: no need for reflection: :: also works on expressions:

(Add-Type @'
public class Foo
{
  public static string Bar(string s) { return s; }
}
'@ -PassThru)::new() | 
  ForEach-Object { $_.GetType()::Bar('1') }

@SeeminglyScience
Copy link
Collaborator

Even just $_::Bar('1') will work there FYI

@SeeminglyScience SeeminglyScience added Issue-Question ideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a WG-Engine core PowerShell engine, interpreter, and runtime Needs-Triage The issue is new and needs to be triaged by a work group. labels Apr 23, 2024
@mklement0
Copy link
Contributor Author

@mklement0 mklement0 closed this as not planned Won't fix, can't repro, duplicate, stale Apr 24, 2024
Copy link
Contributor

microsoft-github-policy-service bot commented Apr 24, 2024

📣 Hey @mklement0, how did we do? We would love to hear your feedback with the link below! 🗣️

🔗 https://aka.ms/PSRepoFeedback

@microsoft-github-policy-service microsoft-github-policy-service bot removed the Needs-Triage The issue is new and needs to be triaged by a work group. label Apr 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Question ideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a WG-Engine core PowerShell engine, interpreter, and runtime
Projects
None yet
Development

No branches or pull requests

4 participants