I would like to create a function in Haxe, to be utilized, from (existing) javascript and php.
In JavaScript I was used to use a Plain object as a map (before es5 Maps), in haxe I would probably need to use Map<String,T> for this behavior.
The problem is that wen compiling into javascript it compiling to a special class having an internal h parameter holding the elements.
What is the preferred method to make a haxe function that could convert a plain js object, or php assoc array, into a string map?
Hello,
there is a object map available in Haxe
While a Map is probably a better choice in many cases, Haxe also supports anonymous structures that work similarly to JS plain objects. If you type the variable as Dynamic
, it basically works the same as JS. If you want the compiler to provide a little more protection for you, you can create a typedef to specify which fields are available.
danielo515
(Daniel Rodríguez Rivero)
December 5, 2022, 5:04pm
4
The problem with typedef is that it generates unwanted fields in the generated object. At least in the Lua target
typedef X = {
var cmd:String;
}
_hx_o({__fields__={cmd=true},cmd="Yo"})
Does it generate unwanted fields if you use Dynamic
instead?
var myObj:Dynamic = {};
myObj.cmd = "Yo";
danielo515
(Daniel Rodríguez Rivero)
December 5, 2022, 7:10pm
6
Yes, because they are created with this function:
local function _hx_e()
return setmetatable({__fields__ = {}}, _hx_obj_mt)
end
Could I make an anonymous structure that is also itirable for optional fields?