class TestIssue {
public static function main() {
var out:Dynamic;
out = new Map<String, Dynamic>();
var kv = {key: "aaaa", value: "bbbb cccc"}
trace(kv);
out.set(kv.key, kv.value);
}
}
Is causing that error
TestIssue.hx:6: { key => aaaa, value => bbbb cccc }
Called from ? line 1
Called from TestIssue.hx line 7
Uncaught exception - Invalid call
In my actual use case, I am getting a different error, the code is too broad to post here
Called from ? line 1
Called from Main.hx line 15
Called from Main.hx line 101
Called from C:\HaxeToolkit\haxe\std/neko/_std/haxe/ds/StringMap.hx line 33
Uncaught exception - $hset
I have some benefit from making it dynamic
But I am wondering what the proper approach would be
It’s probably a good idea to use var out:Map<String, Dynamic>;
For sure then Haxe will know it has the “set” method.
You probably have enough risk of problems having a Dynamic as the value field.
Really really try to avoid Dynamic in your codebase.
Perhaps a Constraints would give you a bit more freedom.
But I suspect this is really what enums were invented for. But they may be too heavy.
enum NotDynamic {
Floaty( f: Float );
Stringy( s: String );
Booly( b: Bool );
}
function main() {
var out = new Map<String, NotDynamic>();
out.set( 'afloat',Floaty(Math.PI));
out.set( 'astring',Stringy('π'));
out.set( 'abool',Booly(true));
for (key => notDynamic in out ){
switch( notDynamic ){
case Floaty( f ):
trace( '$key is float $f' );
case Stringy( s ):
trace( '$key is string $s' );
case Booly( b ):
trace( '$key is bool $b' );
}
}
}