-
Notifications
You must be signed in to change notification settings - Fork 1k
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
[Proposal] ValueEnum - (Random Thought) #1325
Comments
Could you be more specific as to how a |
Maybe I am missing something important, but as far as I was aware, any current What exactly to you expect from a hypothetical |
This is just brainstorming, but I'm taking a step back and looking at it from the idea of not what a current enum is, but how do people want/try to use enum like (real-world) "enumerations". For example, an ordered list of strings is a "string enumeration". A common way people use (or misuse depending on your perspective) enum is as a list of string constants. It provides an easy to use, constrained list at design time.
But, enum is picky about some things. I'm just pulling from distant memory, but there are complexities in using them as constraints on generics, and in attributes. You can't do this
I recall working with attributes and having to use
I've seen proposals for a more "class-like" enum, with things like inheritance.
I wanted to do something with an enum today that I knew I couldn't. It got me thinking about a bunch of discussions and ideas people had about a year ago around enum. In many cases the result was, "good ideas, but can't be done with the current way enum is implemented in the framework". The framework @Unknown6656, Again, this is thinking not about what an enum currently is, but what many people might want from an enum, (or even think an enum is). Having "dottable" lists of values or references, at design time - |
Some of these (like being able to use an Others (like poor performance for various |
That is strictly a compiler limitation, and one that is likely to be lifted. #104
Yes it is. Look at [AttributeUsage(AttributeTargets.All)] // <-- AttributeTargets is an enum
public class MyAttribute : Attribute { }
Which might be interesting, but value types can't be inherited either. There might be ways via syntax candy to pseudo-inherit the existing members of one enum into another, which is something that could be solved through source generators. But that would be a disparate enum. True inheritance would involve classes, heap allocations and all of that overhead to represent what is effectively an integral value.
Also painfully close to There's probably going to be more conversation around enums of other values when pattern matching explores algebraic data types or discriminated unions along with records, like this example in Swift: enum Barcode {
case upc(Int, Int, Int, Int)
case qrCode(String)
}
var productBarcode = Barcode.upc(8, 85909, 51226, 3)
switch productBarcode {
case .upc(let numberSystem, let manufacturer, let product, let check):
print("UPC: \(numberSystem), \(manufacturer), \(product), \(check).")
case .qrCode(let productCode):
print("QR code: \(productCode).")
}
// Prints "UPC: 8, 85909, 51226, 3 |
That doesn't make any sense, If you're talking about a general
That sounds like dotnet/roslyn#9120, which was closed and not moved to this repo ("I don't have confidence that the LDM would likely consider doing this."). How would |
Another possibility is nested/composite enums (#587, #1079). Though I'm not entirely sure how that would be implemented without struct inheritance, or without lowering/magic that may break across assemblies/versions/languages. |
There are many proposals/issues related to Enum. Many of them are constrained by underlying technologies and backwards compatablilty.
ValueTuple
did wonders for the issues withTuple
. Consider taking the same approach with enums by introducing theValueEnum
.The text was updated successfully, but these errors were encountered: