-
-
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
Enum map does not play nice with ValueType #2479
Comments
I"m using Haxe 3.0.1 and compile to JavaScript. |
I suppose our compare function isn"t really valid when it comes to instances or I don"t know what we can do about this in the context of a tree implementation. |
I don"t know the implementation details, but wouldn"t it be possible to have a working compare for enums? |
The problem is that enums can have arbitrary arguments and things can get really messy from there. We don"t define how to instances compare, which naturally extends to enums which have instances (or things like |
Seems like something is wrong with JS way of comparing Class (which are created with functions), which makes it unsuitable for such sorting. You should consider using the class name instead.
|
Another way would be to be able to generate at compile time compare method for enums (and other values) which would deal with Class in a specific way (adding object id as haxe.ds.ObjectMap does) |
ideal use case for compiler generated type classes ;) |
@frabbit you name it! :) |
I"m not sure I understand the problem. So enums have zero or more parameters. We are fine with zero-parameter enums, because they are inherently orderable. If the parameters themselves are orderable too, then a lexicographic ordering should be simple for enum values with parameters, too. If they are not orderable, the compiler could give an error. Would this /solve the problem? |
@lptr the problem comes from Class not being comparable in JS runtime. That"s a quite narrow problem, we use propertly orderable sort, but Class comparison operators are simply broken in JS. It will work with any other value. |
Thanks for the clarification. Are they going to be fixed? |
@lptr fixing it would slow down the whole system since it would require to check if every value is a function and then use a specific comparison function. A possibility would be to either generate at compile-time a type-specific comparison function, or to be able to switch the comparison function if the types contain functions in JS. I"m not very happy with either of the choices, maybe @Simn will have other ideas. In the meanwhile you should be able to use the following class in replacement:
|
Thanks. This indeed looks slower. What would be a way to extract quickly something that I can use as a key instead of |
@lptr what kind of values do you want to reference by type? any value? a specific set? (eg only objects instances) |
Any value indeed. What I use now is a |
@lptr I guess you will have to map every ValueType to a custom ValueType2 that replaces Class value by classes unique ids as shown in my getId example. Then use ValueType2 as key. Sorry for not providing a better solution but as you can understand it"s quite a JS-specific issue that were not aware of before. |
I don"t really know what to do with this issue. I kind of regret adding EnumValueMap because dealing with arbitrary recursive types as keys is a nightmare. Given that we have no map which accepts Class or Enum as key, it"s fair to say that this limitation extends to enums. It"s sad that this means we cannot use ValueType as a key though. |
It seems to me that saying that dealing with this correctly would slow down all js comparisons is an overstatement. We could easily modify |
This is going into the JS internals, so I"ll assign this to Nicolas for now. For what it"s worth, Caue"s idea makes sense to me. |
@waneck that would break native/fast/unsafe comparison when comparing two Dynamics. There"s no Class type in JS that we can change, and we have forbidden ourselves from overriding native JS behaviors to prevent issues with 3rd party libraries. It seems better to me to say that Class in JS is not comparable. We might want to add some documentation about comparisons so I"m assigning to @Simn until it"s closed. |
Actually there"s a real problem with objects and comparisons on js:
So I guess this is something we can/should deal with when comparing Dynamics |
We now disallow using anything involving |
Yes, EnumValueMap does not work correctly if key has object parameter. This test fails on js, but runs fine on cpp. I was going to report this as new issue but found this issue. I think it should be reopened. class FooParam {
public function new() {
}
}
enum EFoo {
Foo(a: FooParam);
}
class Main {
static function assert(v: Bool, m: String) {
if (!v) {
throw m;
}
}
static function main() {
var map: Map<EFoo, Int> = [];
var keys: Array<EFoo> = [for (i in 0...100) EFoo.Foo(new FooParam())];
for (i in 0...keys.length) {
map[keys[i]] = i;
}
for (i in 0...keys.length) {
assert(map[keys[i]] == i, "map[keys[i]] == i, i=$i");
}
}
} |
Looking at |
We want |
|
I"m not sure if this issue is unique to JS. I was trying to build a This was on HashLink. If you are interested I could investigate more. |
Sorry, it turns out that the issue is I was using Anonymous Structures as the Map key (as a pair class), but this doesn"t work as two equal copies of a structure don"t compare to be the same key in ObjectMap. Ignore my comment above. |
I"d like to resolve this issue by merging #9670 |
Have we made any progress on this matter |
If problem is still with this, we can generate classes and methods as |
This code works as expected:
However, if I move
map.set(TClass(Color), "TClass(Color)");
one line up, the code does not findTClass(Color)
anymore:This prints this on the console:
Essentially, not finding
TClass(Color)
anymore.The text was updated successfully, but these errors were encountered: