Please feel free to provide feedback or file bugs here.

Can't use keywords as class member names

Votes from Connect: 8

Original Date Submitted: 8/25/2015 10:53:08 AM

Description:
********Contact Information********
Handle: Joel 'Jaykul' Bennett
Site Name: PowerShell
Feedback ID: 1709557
***************************************

Frequency: PowerShell
Regression: Try to use a keyword as a method:

class Startup {
[void] Configuration([OWin.AppBuilder]$app)
{
$app.UseNancy()
}
}

class Test {
[hashtable]$Options
[void] Switch([string]$option)
{
$Options[$option] = !$Options[$option]
}
}

class Test {
[hashtable]$Options
[bool] If([string]$option)
{
return [bool]$Options[$option]
}
}

Problem Description:
We currently can't define methods or properties with names that match PowerShell keywords. This is silly, since we CAN define functions with those names -- and because of how you can use them, functions with keyword names can cause confusion, but class members cannot!

This is a problem because it prevents us from implementing interfaces or conventions which use these names.
For instance, we cannot do anything with OWin, because we need to implement a class with a "Configuration" method:

class Startup {
[void] Configuration([OWin.AppBuilder]$app)
{
$app.UseNancy()
}
}

Product Studio item created by Connect Synchronizer due to creation of feedback ID 1709557 (http://connect.microsoft.com/PowerShell/feedback/ViewFeedback.aspx?FeedbackID=1709557).

Repro Steps:
Try to use a keyword as a method:

class Startup {
[void] Configuration([OWin.AppBuilder]$app)
{
$app.UseNancy()
}
}

class Test {
[hashtable]$Options
[void] Switch([string]$option)
{
$Options[$option] = !$Options[$option]
}
}

class Test {
[hashtable]$Options
[bool] If([string]$option)
{
return [bool]$Options[$option]
}
}

Expected Results:
There's no reason that PowerShell keywords should be reserved against class method names -- they're not even reserved against function names, where it can **actually** be confusing.

Internal BugId: 15755

8 votes
Sign in
(thinking…)
Password icon
Signed in as (Sign out)

We’ll send you updates on this idea

AdminJoey Aiello [MSFT] (Program Manager, Windows Server) shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

1 comment

Sign in
(thinking…)
Password icon
Signed in as (Sign out)
Submitting...
  • Flavien Michaleczek commented  ·   ·  Flag as inappropriate

    We can't use a lot of words in method of a class.
    But we can use these same words for a class name or a class property.

    By example, this is valid :
    class Exit {
    $Exit = 5
    }

    But this is invalid :
    class Myclass {
    Exit() {}
    }

    So, we can't use a class Name "Exit" in the real world, because it's impossible to use a constructor (but possible to create it without a constructor)

    The normal behavior would be "same restriction" that Add-Member -MemberType ScriptMethod

    By Example, this is valid :
    $object = [psobject]::new() | Add-Member -MemberType ScriptMethod -Name if -Value { "Valid" } -PassThru
    $object.If()

    There is more words we can't use than defined in the reserved word page
    https://technet.microsoft.com/en-us/library/hh847868.aspx?f=255&MSPPError=-2147217396

    The banned word list came from the class "System.Management.Automation.Language.Tokenizer" :
    elseif
    if
    else
    switch
    foreach
    from
    in
    for
    while
    until
    do
    try
    catch
    finally
    trap
    data
    return
    continue
    break
    exit
    throw
    begin
    process
    end
    dynamicparam
    function
    filter
    param
    class
    define
    var
    using
    workflow
    parallel
    sequence
    inlinescript
    configuration
    public
    private
    static
    interface
    enum
    namespace
    module
    type
    assembly
    command
    hidden
    base

    Some words like "Parallel", "Configuration", "Exit", "Filter", "Data", "Base" are too generic to be forbidden.

    This is a serious restriction that can slow down classes adoption for the mass.

    Powershell can't be a "First-class citizen" with this kind of bug.

Feedback and Knowledge Base