-
Notifications
You must be signed in to change notification settings - Fork 1k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Champion "permit methods in enum declarations" #297
Comments
This would allow me to get rid of so much |
One thing that this would probably enable is overriding |
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.) |
@MI3Guy That is a good point. We'll consider this when next we have the opportunity to make CLR changes. |
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 |
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. |
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. |
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, |
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 |
I believe the compiler already treats enums as readonly. |
If we allowed overriding |
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. |
@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;
// ...
} |
@TahirAhmadov Another reason not to emit extension methods is that the C# compiler would be generating a public class name. |
I actually think it's a benefit. |
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];
} |
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 ( |
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 |
I hope you're right. I'd love to get rid of my numerous |
|
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);
} |
Maybe along with a rule that only one enum part can contain implicitly numbered members. |
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. |
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 |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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.The text was updated successfully, but these errors were encountered: