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
[js] @:expose enum to js #4630
Comments
The run-time representation of enums is not defined in Haxe, so exposing them doesn't make a whole lot of sense. You can work with the current representation and extract information that way, but this is not guaranteed to be stable. Either way, this is not really a Haxe issue. Feel free to discuss this on the haxelang google group. |
If you don't have any parameter, you can use abstract enums for that. |
@ncannasse, but using an abstract enum will inline the values, forcing the user to hardcode them on the js side, no? |
@azrafe7 that shouldn't be a problem as long as they are ==, actually an enum with no parameter is guaranteed to be == to the same value. |
What I meant to say is that when exposing an abstract enum the values are not wrapped in an enum/object, so from the js side the user must know in advance what the possible values could be, without any way to discover them (apart from the docs of the original hx, if any). |
To support Javascripter users specifically, you would need to define the values as a static values and turn off full DCE so that they are retained in the code. You can see in the JS output that the statics are retained, selectively copying aspects of the output code you can see that a js user could create meaningful tests using the library while internally the code using Trio could be optimised: TrioJS.Good = 7;
TrioJS.Bad = 666;
TrioJS.Ugly = 300;
Test.main = function() {
var actor = 7;
console.log("Actor is Good is " + Std.string(actor == TrioJS.Good));
}; I don't know if this is a really good approach or how Haxe compile could be adapted to support this. Perhaps it would be note added to documentation if it's a sensible approach, it seems viable but without real case it's difficult to be sure, but seems rather extreme to get the js generator to automatically generate a TrioJS class, and certainly it would probably never be used if a compiler option was added just for this. Tricky question. Abstract Enums are not intended for use by Javascript users so it's not surprising that they have no specific js support. But suggested the work round in case it's useful. |
( You may wonder why 300 is ugly http://www.geeksforgeeks.org/ugly-numbers/ ) |
Azarfe7, ofcourse this does not help with complex enums used in hxDeadalus but I am not sure that they would need to be accessed by the JS user directly, not looked at what gets outputted for them, if the topic was related to the js focused version that someone was optimising for js target. |
@Justinfront, this is not related to hxDaedalus. Sure one could go with manually wrapping the enum with a static class on the haxe side and expose that, but it's somewhat counter intuitive (adding Yeah, I figured out pattern matching is a problem when transpiling, but maybe something can be done to solve this kind of issues when simple/no-parameters enums are involved. Now please don't hate me @Simn and @ncannasse ;). Let's discuss it more. |
Hej all ! |
The use case may not necessarily be inspecting the enum value content. It could be an API function accepting an enum. Enum constructors are just static constants or functions I think it is ok to expose them. |
I think it would be very interesting to have it implemented on JS, if it's not much work, just in order to have something consistent on Haxe targets, even if there are many other ways to achieve what we want without using enums. |
Can we reopen this? As I said, the use case is not to inspect the enum value but provide a way for JS (non-haxe) users to create Haxe enum which can be passed to Haxe-generated APIs. |
Makes sense to me |
I don't know if this is useful, but generally this is how I go about exposing enums for use in js. The idea being that the API "looks" the same, but since it's the static class that is exposed, with the values there, it can be exported and anything it generates can be used an any exposed API functions that use the inner abstract enum.
|
I can't find a way to expose an enum to js, and I see that recently someone else posted on gitter with the same problem.
I'm currently using a hackish solution to achieve it (see this discussion on the mailing list), but I'm wondering if there's a standard way to do it or if it has been intentionally left out.
The text was updated successfully, but these errors were encountered: