-
Notifications
You must be signed in to change notification settings - Fork 59
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
enum abstract
over enum
constructors
#87
base: master
Are you sure you want to change the base?
enum abstract
over enum
constructors
#87
Conversation
Looks like you are editing the template file, instead of copying the template into |
For some more inspiration, I recently learned Ada has a handy feature related to this which lets you specify a subtype as a range of enum values (see "Enumeration subtypes"). As I understand it their enums are more like "traditional" ones rather than ADTs but... |
I like the idea. |
enum abstract Status<T>(Option<T>) to Option<T> {
final Continue = Some;
final End = None;
} This violates the abstraction because There are also some questions regarding pattern matching. For instance, given this enum from the proposal: enum abstract MaybeValue<T>(Option<T>) to Option<T> {
final One = Some(1);
final Two = Some(2);
final Other = Some;
final Nothing = None;
} What should happen if we have: switch (maybeValue) {
case Other(x):
case One:
case Nothing:
} Here, Overall, I'm not opposed to this idea, but the detailed design of this proposal is quite weak and needs some work before I could see myself approving this. |
From my understanding, If I understand correctly, for primitive enum abstract the compiler expects literal values of the underlying type. So in this new feature, when assigning the value, as in
The original idea of this proposal is really just to create a subset of the constructors of the underlying enum, without partial/full application. The example of |
I don't follow where that
But that doesn't mean that the problem doesn't have to be addressed. The cases you create aren't necessarily distinct, e.g. |
enum abstract Status<T>(Option<T>) to Option<T> {
final Continue = Some;
final End = None;
}
$type(Status.Continue); // T -> Status<T>
$type(Option.Some); // T -> Option<T> What I really after is to define a subset of enum constructors, not a subset of enum instance values which should go #86 IMO. Perhaps we need some special metadata to express that intention. |
I think this proposal should be renamed as "abstract enum over enum constructors" to avoid confusion. In the following I am trying to think aloud my understanding of the concepts around enum and enum abstracts and their relationship with this proposal. Any comments welcome. First of all, regarding the
and this proposal is about the 1st set. For the 2nd set, IMO #86 is more relevant. Secondly, regarding the
I think 2a is fine because enum constructor itself is considered constant regardless of its parameter count, i.e. the following code already compiles: enum abstract Foo(Dynamic) {
final A = haxe.ds.Option.None;
final B = haxe.ds.Option.Some;
} I think it is 2b that is the major blocker of this proposal as enum constructors can be of different type depending on their parameters. Thirdly, regarding the typing of enum constructors and enum abstract entries: var value:TheEnumAbstract = TheEnumAbstract.AnyMember; // always hold
var value:TheEnum = TheEnum.AConstructorWithoutParam; // ok
var value:TheEnum = TheEnum.AConstructorWithParam; // not ok But since currently we can't declare a enum abstract entry with parameter, so I consider the behavior between enum and enum abstract consistent. Conclusion: I think we want to keep the restriction that each entry in enum abstract is of the underlying type, such that proposals like #86 is still allowed. But since this restriction blocks this proposal as mentioned, I suggest introducing a specially handled As for how to actually declare an
|
enum abstract
over enum
enum abstract
over enum
constructors
Alternate proposal is to allowing defining constructor function for enum abstracts that would work with top down inference and exhaustiveness check just like normal enums: enum abstract Option<T>( Null<T> ) {
var None = null;
@:constructor public static function Some<T>( v : T ) : Option<T> { return cast v; }
} |
We didn't reach a conclusion on this proposal in the haxe-evolution meeting today. We feel like we should look closer into the problems that are meant to be solved here and then find solutions for those. There have been some alternative suggestions already, but so far it is unclear what to do and how to go about it. |
Rendered Version