Structural subtyping in macro
This function gets @:build
-ed to display JSON objects . It works great when there is only one JSON file or all files have the same field structure. But when the object structures are different I get structural subtyping errors ("Object requires field xxx"). Also, the order of the objects matters sometimes.
I don't understand why it is type-checking; you think they will be announcements and will be left alone. Can anyone see if I am doing something right; otherwise I'll make a question on Github.
public static function build():Array<Field> {
var fields = Context.getBuildFields();
// Object requires field "deep"
var files = ['{"name": "Test1", "id": { "deep" : 5 }}','{"name": "Test0","id": {}}'];
// no error
// var files = ['{"name": "Test0","id": {}}','{"name": "Test1", "id": { "deep" : 5 }}'];
var a:Array<Expr> = [];
for (s in files) {
var o = Context.parseInlineString(s, Context.currentPos());
a.push({ expr : EBinop(OpArrow, macro $v{s}, o), pos : Context.currentPos() });
}
var newfield:Field = {
name : "files", doc : null, meta : [], access : [AStatic, APublic], pos : Context.currentPos(),
// map array decl: [x => y]
kind : FVar(macro : StringMap<Dynamic>, { expr : EArrayDecl(a), pos : Context.currentPos() } )
};
fields.push(newfield);
return fields;
}
I am using Haxe 3.2.0-rc2.
+3
source to share
No one has answered this question yet
Check out similar questions: