Skip to content

Conversation

@sdwheeler
Copy link
Collaborator

@sdwheeler sdwheeler commented Feb 1, 2022

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

  • Repo documentation and configuration (.git/.github/.vscode etc.)
  • Docs build files (.openpublishing.* and build scripts)
  • Docset configuration (docfx.json, mapping, bread, module folder)

Conceptual documentation

  • Files in docs-conceptual

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.

  • Preview content
  • Version 7.2 content
  • Version 7.1 content
  • Version 7.0 content
  • Version 5.1 content

PR Checklist

  • I have read the contributors guide and followed the style and process guidelines
  • PR has a meaningful title
  • PR is targeted at the staging branch
  • All relevant versions updated
  • Includes content related to issues and PRs - see Closing issues using keywords.
  • This PR is ready to merge and is not Work in Progress. If the PR is work in progress,
    please add the prefix WIP: or [WIP] to the beginning of the title and remove the prefix when
    the PR is ready.

@sdwheeler sdwheeler requested a review from daxian-dbw February 1, 2022 21:45
@opbld31
Copy link

opbld31 commented Feb 1, 2022

Docs Build status updates of commit 2b7456c:

✅ Validation status: passed

File Status Preview URL Details
reference/7.3/Microsoft.PowerShell.Core/About/about_Calling_Generic_Methods.md ✅Succeeded View (powershell-7.3)

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:

@opbld33
Copy link

opbld33 commented Feb 1, 2022

Docs Build status updates of commit 884b074:

❌ Validation status: errors

Please follow instructions here which may help to resolve issue.

File Status Preview URL Details
❌Error Details

  • [Error: RuningBuildFailed] Some unexpected errors happened when running build, please open a ticket in https://aka.ms/SiteHelp and include the error report for our team to troubleshoot

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:

@opbld33
Copy link

opbld33 commented Feb 1, 2022

Docs Build status updates of commit 28c84c3:

✅ Validation status: passed

File Status Preview URL Details
reference/7.3/Microsoft.PowerShell.Core/About/about_Calling_Generic_Methods.md ✅Succeeded View (powershell-7.3)

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:

[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.
Copy link
Contributor

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

Copy link
Collaborator Author

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my updated version.

Copy link
Contributor

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example works even without the new generic method feature (see below).
Maybe change it to a code snippet that is enabled by this new feature.

image

Copy link
Collaborator Author

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.

Copy link
Contributor

@daxian-dbw daxian-dbw Feb 2, 2022

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.

Copy link
Collaborator Author

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

Copy link
Contributor

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.

Copy link
Collaborator Author

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

Copy link
Contributor

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:

image

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.

image


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.

Copy link
Collaborator Author

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

@opbld30
Copy link

opbld30 commented Feb 2, 2022

Docs Build status updates of commit 57d5454:

✅ Validation status: passed

File Status Preview URL Details
reference/7.3/Microsoft.PowerShell.Core/About/about_Calling_Generic_Methods.md ✅Succeeded View (powershell-7.3)

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:

@opbld33
Copy link

opbld33 commented Feb 2, 2022

Docs Build status updates of commit 1079c4a:

✅ Validation status: passed

File Status Preview URL Details
reference/7.3/Microsoft.PowerShell.Core/About/about_Calling_Generic_Methods.md ✅Succeeded View (powershell-7.3)

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:

@opbld30
Copy link

opbld30 commented Feb 2, 2022

Docs Build status updates of commit 4ac49c9:

✅ Validation status: passed

File Status Preview URL Details
reference/7.3/Microsoft.PowerShell.Core/About/about_Calling_Generic_Methods.md ✅Succeeded View (powershell-7.3)

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:

@sdwheeler sdwheeler merged commit ba325e4 into MicrosoftDocs:staging Feb 2, 2022
@sdwheeler sdwheeler deleted the sdw-i8533 branch February 3, 2022 17:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add documentation for invoking generic methods with explicit type parameters

6 participants