HaxeFoundation / haxe

Haxe - The Cross-Platform Toolkit
https://haxe.org
6.18k stars 655 forks source link

Map with parameterized key #9023

Open romamik opened 4 years ago

romamik commented 4 years ago
@:generic 
class MyMap<K> {
    public var map: Map<K, Int> = new Map();
    public function new() {}
}

This code does not compile with error

Abstract haxe.ds.Map has no @:to function that accepts haxe.IMap<MyMap.K, Int>

I always thought that this is because underlying type selection occurs before type parameter application. But this is not the case, because I've just discovered that @:remove metadata makes this example compile. So now I'm confused, why it does not just work with @:generic only. Also this is not documented in any way, I've only found this mentioned here: https://github.com/HaxeFoundation/haxe/issues/2537

Also it does not help with abstracts. It actually looks like abstracts cannot be generic at all...

@:generic @:remove
abstract MyMap<K>(Map<K, Int>) {
    public function new() {
        this = new Map();
    }
}
RealyUniqueName commented 4 years ago

I agree we should improve @:remove documentation. I don't have any opinion on @:generic abstract atm, but an abstract over Map won't be supported until @:multiType abstract feature is reworked or replaced with something better.