2015-09-05 36 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上執行一個函數,這是不可能的,這就是錯誤被拋出的原因。