Champion "permit methods in enum declarations" #8987
Replies: 24 comments 3 replies
-
This would allow me to get rid of so much |
Beta Was this translation helpful? Give feedback.
-
One thing that this would probably enable is overriding |
Beta Was this translation helpful? Give feedback.
-
My understanding is that this is not allowed by the CLR spec (I.8.5.2). Are there plans to change that? The following restrictions are listed:
I imagine that removing all the restictions except for the single field and System.Enum requirements should be possible. (Although having events would be kind of useless because of the single field restriction.) |
Beta Was this translation helpful? Give feedback.
-
@MI3Guy That is a good point. We'll consider this when next we have the opportunity to make CLR changes. |
Beta Was this translation helpful? Give feedback.
-
I prefer writing extension method But well, I think what the proposer have explain. We don't need CLR changed. He just propose that we can just transpile enum method into extension method |
Beta Was this translation helpful? Give feedback.
-
This feature won't truly shine until C# also supports enum inheritance and enum generic constraints. I run into enum limitations all the time in interop. |
Beta Was this translation helpful? Give feedback.
-
I think this is probably a feature best left for ADTs. Sure, methods could be added to enums but once C# has types such as discriminated unions (which can have methods) I think the use of raw enums will decrease dramatically. |
Beta Was this translation helpful? Give feedback.
-
This is something that is very much needed. I don't like having to keep it all as extension methods; it's counter intuitive and requires more typing, |
Beta Was this translation helpful? Give feedback.
-
Maybe not entirely related, but probably worth discussing in this context: Is it possible to start emitting Admittedly, that may be a bit much. Currently, the |
Beta Was this translation helpful? Give feedback.
-
I believe the compiler already treats enums as readonly. |
Beta Was this translation helpful? Give feedback.
-
If we allowed overriding |
Beta Was this translation helpful? Give feedback.
-
Relaxing the CLR spec (or generally making changes to the CLR in tandem with C# releases) is something that has been happening. The appetite to do this is back now that the only people affected by CLR changes (in .NET Core/5+) are people who overrode defaults and asked to be affected. |
Beta Was this translation helpful? Give feedback.
-
@jnm2 hopefully allowing to override Regarding syntax, we need to decide where enum options stop and other members start. I propose semicolon: enum Color
{
Red,
Green,
Blue;
public override string ToString()=>"asd";
public void Foo() { ... }
public int Prop => ((int)this) + 1;
// ...
} |
Beta Was this translation helpful? Give feedback.
-
@TahirAhmadov Another reason not to emit extension methods is that the C# compiler would be generating a public class name. |
Beta Was this translation helpful? Give feedback.
-
I actually think it's a benefit. |
Beta Was this translation helpful? Give feedback.
-
Another thought I had: together with methods, enums should allow declaring static fields; then scenarios like below become possible: enum Foo
{
A,
B;
static readonly string[] _displayNames = new[] { "Alpha", "Beta" };
public string DisplayName => _displayNames[(int)this];
} |
Beta Was this translation helpful? Give feedback.
-
I found an Oracle patent for object-oriented enums mentioned in other discussions. Does that mean this is legally blocked until the patent's expiration date ( |
Beta Was this translation helpful? Give feedback.
-
That patent covers an implementation of enums as instances of a reference type with a somewhat specific shape. IANAL, I don't think allowing arbitrary methods within an enum declaration would fall afoul of that patent |
Beta Was this translation helpful? Give feedback.
-
I hope you're right. I'd love to get rid of my numerous |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Just dropping a thought about this feature here, would it be nicer to force separating the other members of the enum into a different declaration? That would also require support for partial enums. In each partial enum declaration, you can only have either normal members, or the enum's fields. That means, defining such an enum would look like this: public partial enum S
{
None,
A,
B,
C,
}
partial enum S
{
public bool IsValid => this is A or B;
// would be nice to automatically access the max value as a compile-time constant
// without having to define it manually everywhere
// also for min value and total count
private const int MaxValue = (int)C + 1;
// either directly use value__ or a friendlier identifier to access the internal value
public S Increment() => (S)((this.value__ + 1) % this.MaxValue);
} |
Beta Was this translation helpful? Give feedback.
-
Maybe along with a rule that only one enum part can contain implicitly numbered members. |
Beta Was this translation helpful? Give feedback.
-
That touches the proposal about partial enums and the declaration of their fields, but definitely some rules must be enforced to ensure that field declaration doesn't go out of hand. |
Beta Was this translation helpful? Give feedback.
-
This would eliminate the common mistake where the developer forgets to call an extension method during string interpolation, causing the enum's enum EmployeeType { Manager, Staff }
static class EmployeeTypeExtensions
{
static string GetTitle(this EmployeeType employeeType) => employeeType switch
{
EmployeeType.Manager => "Team Leader",
EmployeeType.Staff => "Team Member"
}
}
static string GetEmployeeTitle(Employee employee)
{
string titleVerbiage = $"Employee title: {employee.EmployeeType}"; // oops, should've been employee.EmployeeType.GetTitle()
} Have to be careful though. Override |
Beta Was this translation helpful? Give feedback.
-
Champion issue: #8989
See also dotnet/roslyn#3704
Reminder (since we don't yet have a work items list for this):
class
orstruct
contexts, but notenum
. Maybe they should be updated to allowenum
too. For example, SkipLocalsInitAttribute.Beta Was this translation helpful? Give feedback.
All reactions