2015-09-05 62 views
2

我在Haxe遇到了一些问题,实际上无法解决它。我无法找到任何有关宏的优秀文档,所以如果你知道的话,如果你分享,我会很感激。Haxe宏:导致无效访问静态

有代码:

class ProfilesContainerBuilder { 
#if macro 
    public static function build() : ComplexType { 
     var fields = new Array<Field>(); 
     switch (Context.getLocalType()) { 
      case TInst(_, [t1]): 
       fields.push({ 
        name: "get", 
        access: [APublic,AStatic], 
        pos: Context.currentPos(), 
        kind: FieldType.FVar(Context.getLocalType().toComplexType(),null) 
       }); 
       fields.push({ 
        name: "profile", 
        access: [APublic,AStatic], 
        pos: Context.currentPos(), 
        kind: FieldType.FVar(t1.toComplexType(),null) 
       }); 
       fields.push({ 
        name: "_getProfile", 
        access: [], 
        pos: Context.currentPos(), 
        kind: FieldType.FFun({ 
         args: [], 
         expr: { pos: Context.currentPos(), expr: EReturn(
          macro $i{t1.getClass().name+'.fromDynamic(Json.parse(Assets.getText("profiles/"+_activeProfile+".json")))'})}, 
         params: [], 
         ret: t1.toComplexType() 
        }) 
       }); 
      default: 
       Context.error("Class expected", Context.currentPos()); 
     } 
     return TAnonymous(fields); 
    } 
#end 
} 

@:remove @:genericBuild(brocchinia.io.ressource.ProfilesContainerBuilder.build()) 
extern interface ProfilesContainer<T> { } 

而在这之后,有给定的错误,在谁实现建造接口的类:

C:/HaxeToolkit/haxe/lib/brocchinia/0,1,0/brocchinia/io/ressource/Ressource.hx:27: lines 27-114 : Invalid access static 

知道了我从静态和本地函数访问变量get,profile和函数_getProfile。没有宏的部分,它很好。

经过许多小时的研究,我没有发现问题,所以,谢谢你的期待。

回答

2

我发现这个问题,我错误地理解了泛型构造的工作,这要感谢所有读过的人。

实际上,代码在类型T上执行一个函数,这是不可能的,这就是错误被抛出的原因。