Skip to content

Amend "about_Methods" to contrast the syntax differences with argument mode, show pitfalls #2308

Closed

Description

Follow-up from a discussion at PowerShell/PowerShell#6576.

Methods calls are like command/function calls at a certain level of abstraction, so users can get confused between the distinct syntax requirements:

From the perspective of a shell user mostly familiar with command syntax (argument mode), the tricky differences are:

  • parentheses are required (vs. whitespace in argument mode)
  • arguments must be ,-separated (vs. whitespace in argument mode; in argument mode, , constructs arrays)
  • bareword arguments (unquoted strings) aren't allowed

Conversely, for someone coming from the developer world, calling your own helper functions such as function foo ($bar, $baz) { ... } as foo('one', 'two') is a real temptation, with a rude awakening.

I suggest amending about_Methods as follows:

  • Mentioning these pitfalls.

  • Also making it clear that method calls are limited to expression mode and are essentially a
    pass-through feature to the realm PowerShell is built on, the .NET framework, which is a realm with different rules and syntax.
    Add a reference to about_Parsing

On a side note, I also suggest revise the following confusing sentence re member enumeration which is present not only in about_Methods, but also about_Properties:

If the method [/ property] exists on the individual objects and on the collection, PowerShell does not alter the result.

This should read something like:

If the method / property exists on both the individual objects and on the collection itself, it is (only) the collection's member that is invoked.

To illustrate this with a concrete example:

PS> (@{ length = 'foo' }, @{ length = 'bar'}).length
2   # Collection-level `.Length` property took precedence

Version(s) of document impacted

  • Impacts 6.1 document
  • Impacts 6.0 document
  • Impacts 5.1 document
  • Impacts 5.0 document
  • Impacts 4.0 document
  • Impacts 3.0 document

Activity

  1. changed the title [-]Amend "about_Methods" to contrast the syntax differences with argument mode[/-] [+]Amend "about_Methods" to contrast the syntax differences with argument mode, show pitfalls[/+] on Apr 8, 2018
  2. added a commit that references this issue on Aug 21, 2018
    bf04e6e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Pri2

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions