You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Class : Methods and properties can't have the same name #8235
Committee-ReviewedPS-Committee has reviewed this and made a decisionIn-PRIndicates that a PR is out for the issueIssue-BugIssue has been identified as a bug in the productResolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime
Get an error saying you can't have a property and a method with the same name
Actual behavior
PS C:\EDICore>$obj|Get-Member
TypeName: TestClass
Name MemberType Definition
------------------------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
Prop Method string Prop()
ToString Method string ToString()
PS C:\EDICore>$obj.Prop()
Method invocation failed because [TestClass] does not contain a method named 'Prop'.
At line:1 char:1+$obj.Prop()
+ ~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Environment data
>$PSVersionTable
Name Value
---------
PSVersion 6.1.0
PSEdition Core
GitCommitId 6.1.0
OS Microsoft Windows 6.1.7601 S
Platform Win32NT
PSCompatibleVersions {1.0,2.0,3.0,4.0...}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
...
Following the rules of the CTS, it is possible for duplicate names to be defined in the same scope
as long as they differ in either kind (field, method, etc.) or signature.
...
CLS Rule 5
All names introduced in a CLS-compliant scope shall be distinct independent of
kind, except where the names are identical and resolved via overloading. That is, while the CTS
allows a single type to use the same name for a method and a field, the CLS does not.
I think it's fair to say that CLS compliance is no longer a consideration. But, given that C# doesn't allow this, I strongly suspect that the current behaviour is by design. Maybe Bruce Payette (@BrucePay) or Jason Shirk (@lzybkr) can shed some light there?
The fact that the method Prop() is actually registered looks like a bug though -- we should probably fix that and turn this into a specific parse-time error (e.g. Multiple class members with the same name are not allowed ☝️).
Reacted by Michael Klement, Rain Sallow (/u/ta11ow) and Keith Hill
Allowing members of different types to have the same name is asking for trouble with no obvious benefit (and wer're not breaking anything, given that the method cannot be called).
Note that C# does allow names that differ in casing, which we should disallow as well, given PowerShell's case-insensitive nature.
Michael Klement (@mklement0) moving the mutual check back to TypeDefiner (DefineProperty() and DefineMethod() respectively) makes it way faster and avoids attempts at generating invalid/unusable types should someone pass in an AST generated by a different parser (or by hand)
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commented on Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commented on Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
Committee-ReviewedPS-Committee has reviewed this and made a decisionIn-PRIndicates that a PR is out for the issueIssue-BugIssue has been identified as a bug in the productResolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime
Activity
iSazonov commentedon Nov 13, 2018
/cc Rob Holt (@rjmholt) For your collection.
rjmholt commentedon Nov 13, 2018
C# disallows this; try compiling the following:
From The C# Language Specification - ECMA 334:
That doesn't necessarily mean PowerShell must also, although I think it's indicative.
The Common Language Infrastructure Specification - ECMA 335 has the following to say:
I think it's fair to say that CLS compliance is no longer a consideration. But, given that C# doesn't allow this, I strongly suspect that the current behaviour is by design. Maybe Bruce Payette (@BrucePay) or Jason Shirk (@lzybkr) can shed some light there?
rjmholt commentedon Nov 13, 2018
The fact that the method
Prop()is actually registered looks like a bug though -- we should probably fix that and turn this into a specific parse-time error (e.g.Multiple class members with the same name are not allowed ☝️).rjmholt commentedon Nov 13, 2018
/cc @PowerShell/powershell-committee for discussion
mklement0 commentedon Nov 13, 2018
Rob Holt (@rjmholt): +1 on the parse-time error.
Allowing members of different types to have the same name is asking for trouble with no obvious benefit (and wer're not breaking anything, given that the method cannot be called).
Note that C# does allow names that differ in casing, which we should disallow as well, given PowerShell's case-insensitive nature.
Disallow class definitions with duplicate member names
IISResetMe commentedon Nov 24, 2018
Michael Klement (@mklement0) Rob Holt (@rjmholt) looks like this is fairly straightforward to catch at parse-time: IISResetMe@bfd4be7
mklement0 commentedon Nov 25, 2018
Cool, Mathias R. Jessen (@IISResetMe), thanks for taking this on.
Sounds like Jason Shirk (@lzybkr) is already advising you on the implementation (which I'd be ill-equipped to do anyway).
One suggestion re the user-facing part: How about mimicking the wording of the C# error message, so as to include the type name too?
Though spelling out
method or propertycan't hurt:IISResetMe commentedon Nov 27, 2018
Michael Klement (@mklement0) moving the mutual check back to
TypeDefiner(DefineProperty()andDefineMethod()respectively) makes it way faster and avoids attempts at generating invalid/unusable types should someone pass in an AST generated by a different parser (or by hand)IISResetMe@5404f94#diff-640a7359770582f83b945ef2a276d2de
SteveL-MSFT commentedon Jan 23, 2019
The PR was already reviewed by @PowerShell/powershell-committee so updating label #8372 (comment)
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 23, 2023
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.