-
-
Notifications
You must be signed in to change notification settings - Fork 661
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
Add empty enum as haxe.Unit #11563
base: development
Are you sure you want to change the base?
Add empty enum as haxe.Unit #11563
Conversation
I was thinking that maybe having something like |
I was thinking about trying to hide the |
How will that work with null safety? |
Does null-safety consider enums not-nullable? In that case we'll need a meta to explicitly mark enums as nullable. |
I think so https://try.haxe.org/#249624dF |
Ah nice, it's good that it does that. But yes, a metadata specifically for enums makes sense to me. Enums are finite sets of values, and it would be good to be able to explicitly state that |
Imo having And it also breaks something like this: class Event<T> {
/*
returns the last event, or null if no event has been dispatched yet
*/
function getLastValue():Null<T>;
function dispatch(val: T);
}
var notifier = new Event<haxe.Unit>();
assert(notifier.getLastValue() == null);
notifier.dispatch(null);
assert(notifier.getLastValue() != null); // error, the last value is still null (Of course this example also breaks if you were to do |
I disagree specifically for enums. Enums can be understood as sets, and whether or not a value is in a set is a property of the set itself. By extension, valid set constructors are defined on the set itself, and this may include In contrast, Having said that, your example does give me pause. As you say, this would only become relevant if we had a constraint for type parameters to make them not-nullable. This does sound like a good idea regardless of what we're doing here, and I feel like you don't make the most relevant part clear enough: If we had that type of constraint on Under these circumstances, it seems like a better idea to flip the script: Instead of adding The main problem with that approach is that we have to give that constructor a name. And I'm not even joking here, I always find it giga difficult to remember how such things are named, which is part of the reason I was liking this |
Imo that overloads the meaning of
enum Unit {
Unit;
} seems like the most logical option? Alternatively, introduce tuples and use the empty tuple as unit type. |
I might have a rather pattern-matcher-centric view of this because in there I pretty much have to treat
This would be fine by me. |
This has been discussed a lot in the past, so my apologies if I'm oversimplifying something here. Given that
null
is a thing, the most straightforward way I can think of to implement a unit type is to have an enum without any constructors, which then can only ever benull
.What am I missing?