Powershell: Given that methods in custom classes do not support optional parameters (with default values), attempting to define them should fail

Created on 22 May 2019  路  6Comments  路  Source: PowerShell/PowerShell

Note: If #9701 is implemented, this issue becomes moot.

Steps to reproduce

class Foo {
  # This method definition shouldn't be permitted, because optional parameters
  # aren't supported.
  [string] Bar($Baz = 'Bam') { return $Baz }
}
[Foo]::new().Bar()

Expected behavior

The class definition should fail, given that optional parameters aren't supported.

Actual behavior

The class definition succeeds, but the argument-less invocation of the method fails.

Cannot find an overload for "Bar" and the argument count: "0".

Environment data

PowerShell Core 6.2.1
Issue-Question Resolution-Won't Fix

Most helpful comment

Yeah, in truth if we're going to be poking around here I'd rather we make this work properly.

All 6 comments

If we implement this error message it will 75% from implementing the feature :-)

Yeah, in truth if we're going to be poking around here I'd rather we make this work properly.

I didn't realize that was on the menu.

I'll have the feature then, please :)

Maybe reopen with the feature request?

Good idea; please see #9701

@mklement0 Thanks! I think we can close the issue.

Was this page helpful?
0 / 5 - 0 ratings