HaxeFoundation / haxe

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

ObjectMap doesn't support functions as keys #2412

Closed ofrzeta closed 10 years ago

ofrzeta commented 10 years ago

functions as keys are not possible with the ObjectMap implementation (in Neko?) because the getID() calls fail.

sledorze commented 10 years ago

Functions are not values; so this behavior is totally correct.

ncannasse commented 10 years ago

Ah, I think that the issue was actually fixed when we added the missing constraint {} on ObjectMap, I was surprised that it was allowed to create Map with functions as keys.

Simn commented 10 years ago

There is a FunctionMap implementation in tinkerbell if you really need it: https://github.com/back2dos/tinkerbell/blob/master/src/tink/collections/maps/FunctionMap.hx

ofrzeta commented 10 years ago

I don't see how that constraint works - at least when I am using a Dynamic type. When I compile the following the code, it get an exception

import haxe.ds.ObjectMap;

class Test {
    static function myfunc(param: Int) {
    }

    static function main() {
        var targetLibraries:ObjectMap <Dynamic, Int> = new ObjectMap<Dynamic, Int> ();
    targetLibraries.set (myfunc, 1);
        trace(targetLibraries);
    }
}
ncannasse commented 10 years ago

ObjectMap is only supposed to contain objects, and functions are not objects in Haxe. In your example this goes unoticed because you're using Dynamic, which is unsafe. Use {} (the empty object which works with all objects) and you'll see the error displayed.