-
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
Default value for enum parameters #27
Comments
I like this, every time we strip null possibilities from core language is a big win and lead to leaner code. +1 |
Since enum ctors with args are actually functions, maybe we can support this easily? |
I'd like to see this implemented too. |
+1 @sebthom You could use |
I don't think this could be implemented without significant changes to how we handle default values. In our implementation, the default values are not part of the function type, but of the function itself. Enum constructors to not have actual functions, they are only typed as such. This would only work if we did call-site replacements, but we don't. The only alternative is to respect this in every generator. |
On all platforms I've checked, they are actual functions. What's more, the type system also treats them as such, e.g. |
Look, I'm not making this up to annoy you or avoid implementing this feature. You can look at haxe/macro/Type.hx and verify for yourself that the only place default values show up is |
Sorry, I misunderstood. We're in agreement after all: if the default values are actually known, there is no problem.
Regrettably, upon following your invitation I failed to verify your claim. As it turns out you already introduced a compile time representation of default values. It's not a pretty one, but it is sufficient and could be used for enums just as well. Just saying ... |
I added that because we wanted to have the original syntax for generating documentation. For generating output, we need a properly typed representation and we cannot store that in metadata. I also wouldn't want to rely on such a hack to generate code in the first place. So the only option is change our representation of enum fields. I'm not saying it can't be done, but I guarantee you it's more involved than you make it seem. |
Enums are now such a proeminent and powerful feature of Haxe (look at all the powerful variants of |
There are two main related problems here:
This might sound like premature optimization, but I really don't think that an implementation which wraps basic values only to then null-check and unwrap them would be acceptable. Edit: Just to make this clear, all this bullshit is only required in order to support the dynamic/reflection paths. If we did call-site replacement of default values then both problems wouldn't exist. That's not how Haxe is designed though and it then would indeed be surprising if default values didn't work through reflection nonsense. |
Reflection is not really the problem here (if it were I would suggest solving this in Of course I'm all for a fast path for call site replacement, if possible. I was also gonna suggest to use native default values on the platforms that support it, but the more I think about it, the more I wonder how well that can even fit with our beloved arg skipping. |
You're right, I was somehow thinking that it would turn the type into not having optional arguments, but it doesn't. In that case this is all doomed anyway. Plus, I don't want to implement something that would change |
I was surprised to find that while
is possible, this is not:
Right now I have to do additional null-checks on enums with optional parameters like so
instead of simply:
Therefore I would request adding default value behaviour for enums as specified here https://haxe.org/manual/types-function-default-values.html
The text was updated successfully, but these errors were encountered: