How can I define a typedef which implements Dynamic

122 views
Skip to first unread message

Adrian Veith

unread,
Nov 5, 2014, 11:43:53 AM11/5/14
to haxe...@googlegroups.com
most of the times, when I wrap a JS library I have this problem to pass an "option" object to an initializer or other function. To define the "option" typesafe I generate a typedef with optional fields - this works perfect. But sometimes the field-names of this "option" object have non valid characters. In the example Option can have the fields foo, bar, and "x-bar". I can't define a matching typedef, but what's worse - I can't pass an anonymous object with field "x-bar" to new. 

typedef Option = {
    ? foo: Int,
    ? bar: Int,
    //? "x-bar": Int,
}
    
extern class JSSome {
    public function new(o: Option);
}    
class Test {
    static function main() {
         var j = new JSSome({foo:12, "x-bar":13});
    }
}

Is there a way to either define the typedef with fieldnames in parenthesis (better) or to mark the typedef as Dynamic for the not defined fieldnames ?
 
any solutions ?

thanks, Adrian.

Juraj Kirchheim

unread,
Nov 5, 2014, 12:08:07 PM11/5/14
to haxe...@googlegroups.com
This is interesting. When string fields in anonymous objects where first defined, they were ignored by the typer, as also suggested by the manual: http://haxe.org/manual/types-structure-json.html

So I would call that a bug and suggest filing an issue.

For the time being, this should solve your problem (relying on how open vs. closed structures are treated by the typer:

   var o = { foo: 12, "x-bar": 13 };
   var j = new JSSome(o);

Also, here's how you could go typesafe: http://try.haxe.org/#8BbdF

Best,
Juraj

--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.

Adrian Veith

unread,
Nov 6, 2014, 1:58:22 AM11/6/14
to haxe...@googlegroups.com
Am 05.11.2014 um 18:08 schrieb Juraj Kirchheim:
This is interesting. When string fields in anonymous objects where first defined, they were ignored by the typer, as also suggested by the manual: http://haxe.org/manual/types-structure-json.html

So I would call that a bug and suggest filing an issue.

For the time being, this should solve your problem (relying on how open vs. closed structures are treated by the typer:

   var o = { foo: 12, "x-bar": 13 };
   var j = new JSSome(o);

Also, here's how you could go typesafe: http://try.haxe.org/#8BbdF

Cool, that's it - thanks, Adrian.

Adrian Veith

unread,
Nov 6, 2014, 5:23:09 AM11/6/14
to haxe...@googlegroups.com

Am 05.11.2014 um 18:08 schrieb Juraj Kirchheim:
This is interesting. When string fields in anonymous objects where first defined, they were ignored by the typer, as also suggested by the manual: http://haxe.org/manual/types-structure-json.html

So I would call that a bug and suggest filing an issue.


I reported this as a bug. But there are some other oddities which I am unsure if they are bugs or intended:

typedef Record = {
    recid: Int,
}

class Test {
    static function getRecord(r: Record) {
        trace(r.recid);
    }
    static public function main() {
        var r = { recid: 12, "x-foo": "Hi" };
        getRecord(r);                                   // works
        r = { recid: 12, "x-foo": "Hi" };           // throws { recid : Int } has extra field x-foo
        var r2 = { recid:13, "foo": "Hi"};        // throws { recid : Int, foo : String } should be Record
        getRecord(r2);
    }
}   

it's not very intuitive I would say ...

If the last one works, this would be an ideal solution (quote the fields) for the situation where you have to pass a structure with more unknown fields like in:

var records: Array<Record> = [
    { recid: 1, "fieldName": "Obama", "fieldAddress": "White House"},
    { recid: 2, "fieldName": "Putin", "fieldAddress": "Kreml"},
];
Reply all
Reply to author
Forward
0 new messages