-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add a parse
static or factory ctor to Enum
#33244
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
Comments
Given:
Is it enough to be able to do class A<T> {
var a = Enum.parse(T, "hello"); // Better hope T is an enum type with a "hello" tag
} and dynamic mkHello<T>() => Enum.Parse(T, "hello"); |
Honestly, I'd prefer `static Day tryParse(String value, bool ignoreCase)`
Obviously, if the enum has 'bob' and 'BOB', passing ('bob', true) throws...
…On Fri, May 25, 2018 at 5:50 PM Leaf Petersen ***@***.***> wrote:
Given:
enum Day {
monday, tuesday, wednesday, thursday, friday, saturday, sunday
}
It it enough to be able to do Day.parse("monday")? Or do you want to be
able to say Enum.parse(Day, "monday")? Note that the latter is strictly
more powerful, since I can presumably do
class A<T> {
var a = Enum.parse(T, "hello"); // Better hope T is an enum type with a "hello" tag
}
and
dynamic mkHello<T>() => Enum.Parse(T, "hello");
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#33244 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AABCimm9N00Y2YhWEm1S0GAokBkN3jPAks5t2KbigaJpZM4UOtMT>
.
|
parse
static or factor ctor to Enumparse
static or factory ctor to Enum
I'm not very happy about conflating source names and strings. Adding members to enum classes takes up namespace for values, so adding a The All in all, I think we should do a proper work-over on enums rather than add features piecemeally. |
One, non-breaking, option is to change the Day.values.byName("monday") // if `byName` is a Day Function(String)
// or
Day.values.byName["monday"] // if `byName` is a Map<String,Day>. I would only accept the name that occurs in (I'd prefer to change |
I would also suggest to allow developers to write custom parse methods inside enum itself:
|
Allowing static members inside an We might want to allow instance members too, and have something closer to, say, Java's enum declarations, but that's a much larger change. |
Having Java-like enums would really be perfect. But if static functions are more trivial - I'd say to add them first. It will significantly reduce global namespace pollution (aka |
I like the idea of adding static members to an enum. Then automatically adding something like |
This is what works best while this is not implemented:
Use it like this:
But it would be better if we could write:
Why can't we write |
Because there currently is no such |
Now there is an enum class. |
Mind elaborating on that? I can't find anything on DartPad or the docs about it. |
It is in 2.14.0, every enum extends/implements Enum class. |
And since 2.15, there is a:
|
I believe that the per-enum support for this feature is covered quite well by However, as @leafpetersen mentioned here, the type-abstracting version of this feature is more powerful:
dynamic mkHello<T>() => Enum.Parse(T, "hello"); Today we have enumerated types with type arguments, and we'd need to consider what it means when the value of The most important remaining discussion in this issue would then be whether any such type-abstracting features would fit into the language. Presumably, such features could only be implemented if the language were to introduce support for iterating over all relevant types (here: all enumerated types) in the current program. This could probably be done by generating some code at link time (whatever that means, for each platform and configuration). However, I suspect that this kind of feature could be unable to coexist peacefully with tree shaking. Conversely, if we do overcome the difficulties with typing and tree shaking, why wouldn't we open up the language to support other type-abstracting features based on user-defined code? This might be handled via static meta-programming, if we support iterating over all those types (using some kind of query over all types in the current program) and generate a snippet of code for each of them. But in that case we might also be able to support a function like My conclusion is that this issue could serve as a source of inspiration for some really interesting discussions about static meta-programming, and about the borderline between statically known types and run-time objects. However, I also think that those topics are so different from the original scope of this issue that they should be handled in other issues. @kevmoo, WDYT? Can we close this issue, and ask each contributor to the discussion to create new issues if they wish to explore any of those spin-off topics? |
I'll live w/ the current implementation, but I'd argue discoverability is still lacking. It'd be great to have a |
Yes, it's not obvious. I don't think it's easy to come up with a general solution (perhaps a macro), but if we're willing to write just one line of code per enum then we can do this: enum E {
one,
two;
static final parse = values.byName;
}
void main() => print(E.parse('one')); |
general solution: Add it to the language spec for `enum`!!!
…On Mon, Jul 18, 2022 at 2:47 PM Erik Ernst ***@***.***> wrote:
discoverability is still lacking
Yes, it's not obvious. I don't think it's easy to come up with a general
solution (perhaps a macro), but if we're willing to write just one line of
code per enum then we can do this:
enum E {
one,
two;
static final parse = values.byName;
}
void main() => print(E.parse('one'));
—
Reply to this email directly, view it on GitHub
<#33244 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAEFCQ6QAIHLSS747VY2WTVUXGHZANCNFSM4FB22MJQ>
.
You are receiving this because you modified the open/close state.Message
ID: ***@***.***>
|
C# has it: https://docs.microsoft.com/en-us/dotnet/api/system.enum.parse?view=netframework-4.7.2
The opposite of #30021 & https://github.com/dart-lang/sdk/issues/21712 – where we want the simple string name of the value.
Related to #21966 as this makes interop VERY hard. Have to do a lot of messy string tricks
The text was updated successfully, but these errors were encountered: