-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Fixes #8533 - Add doc for using generic methods #8554
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
Conversation
|
Docs Build status updates of commit 2b7456c: ✅ Validation status: passed
For more details, please refer to the build report. Note: Broken links written as relative paths are included in the above build report. For broken links written as absolute paths or external URLs, see the broken link report. For any questions, please:
|
|
Docs Build status updates of commit 884b074: ❌ Validation status: errorsPlease follow instructions here which may help to resolve issue.
For more details, please refer to the build report. If you see build warnings/errors with permission issues, it might be due to single sign-on (SSO) enabled on Microsoft's GitHub organizations. Please follow instructions here to re-authorize your GitHub account to Docs Build. Note: Broken links written as relative paths are included in the above build report. For broken links written as absolute paths or external URLs, see the broken link report. Note: Your PR may contain errors or warnings unrelated to the files you changed. This happens when external dependencies like GitHub alias, Microsoft alias, cross repo links are updated. Please use these instructions to resolve them. For any questions, please:
|
|
Docs Build status updates of commit 28c84c3: ✅ Validation status: passed
For more details, please refer to the build report. Note: Broken links written as relative paths are included in the above build report. For broken links written as absolute paths or external URLs, see the broken link report. For any questions, please:
|
reference/7.3/Microsoft.PowerShell.Core/About/about_Calling_Generic_Methods.md
Show resolved
Hide resolved
| [Invoking generic methods on non-generic classes in PowerShell](https://www.leeholmes.com/invoking-generic-methods-on-non-generic-classes-in-powershell/). | ||
|
|
||
| A generic method is a method with two parameter lists: a list of generic type parameters and a list | ||
| of formal parameters. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not necessarily a list of parameters. e.g. Array.Empty<XXX>()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean, the formal parameter list could be empty? So zero or more parameters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my updated version.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean, the formal parameter list could be empty? So zero or more parameters.
Yes, it could be 0 or more method arguments.
| [math]::Pow($item, 3) | ||
| } | ||
| ) | ||
| $result |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was one of the pester tests. I tried this in 7.3-preview.1 it does not work.
ParserError:
Line |
1 | $result = [System.Linq.Enumerable]::Select[int, float](
| ~
| Array index expression is missing or not valid.
I tested in a current build of master. It works there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without the [int, float] part. Please take a closer look at the screenshot above :)
PowerShell is able to infer the generic argument in this case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I see that now. The example, as written, is enabled by the new feature. But, as you point out, you can remove types and it works. Can anyone provide a better example?
cc: @vexx32
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently the main things that are strictly enabled by this are methods with zero arguments for PS to infer from, for example:
[System.Linq.Enumerable]::Empty[string]()
[array]::Empty[int]()While it's not yet the case, it could also be used to improve generic method resolution to require less casts (in theory). For example, currently to call [System.Linq.Enumerable]::Select() you have to cast some or all of the arguments to specific types as you have above.
For example, it should be possible to reduce the call above to [System.Linq.Enumerable]::Select[int, float]($list, { <# script block stuff #> }) and avoid the additional casts which are currently necessary.
I think the example as written is fine to illustrate the usage, even if it's not strictly required in all cases currently. Some folks may well just use it to be explicit about exactly what they're calling.
Method resolution can be very complicated, and there can be cases where a method may have both generic and non-generic overloads, where PS could have trouble resolving or may resolve incorrectly (from the user's perspective) without the explicit generic method arguments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without a better example, I think this illustrates the point (@vexx32 said).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For example, it should be possible to reduce the call above to [System.Linq.Enumerable]::Select[int, float]($list, { <# script block stuff #> }) and avoid the additional casts which are currently necessary.
This doesn't work for Select, because there is still ambiguity in the type of the delegate as an argument. See the screenshot below:
But it works for IEnumerable.Any -- when specifying the generic type argument ([int] in this case), no need to have explicit cast for the script block.
I'm fine to keep the current example, or use the Any method as an example (it's up to you @sdwheeler). But please add another one for [array]::Empty[int](), because that's the scenario enabled by this new feature, which doesn't work before.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. Will add [Array]::Empty[int]().
|
Docs Build status updates of commit 57d5454: ✅ Validation status: passed
For more details, please refer to the build report. Note: Broken links written as relative paths are included in the above build report. For broken links written as absolute paths or external URLs, see the broken link report. For any questions, please:
|
|
Docs Build status updates of commit 1079c4a: ✅ Validation status: passed
For more details, please refer to the build report. Note: Broken links written as relative paths are included in the above build report. For broken links written as absolute paths or external URLs, see the broken link report. For any questions, please:
|
|
Docs Build status updates of commit 4ac49c9: ✅ Validation status: passed
For more details, please refer to the build report. Note: Broken links written as relative paths are included in the above build report. For broken links written as absolute paths or external URLs, see the broken link report. For any questions, please:
|



PR Summary
Fixes AB#1914169 - Fixes #8533 - Add doc for using generic methods
PR Context
Check the boxes below to indicate the content affected by this PR.
Repository or docset configuration
Conceptual documentation
Cmdlet reference & about_ topics
When changing cmdlet reference or about_ topics, the changes should be copied to all
relevant versions. Check the boxes below to indicate the versions affected by this change.
PR Checklist
please add the prefix
WIP:or[WIP]to the beginning of the title and remove the prefix whenthe PR is ready.