2017-01-18 1 views
2

Considérez ce code:construction génériques ajout de champs

http://try.haxe.org/#5AD6e

class Test 
{ 
    public static function main() 
    { 
     var foo = new FluentFoo(null, { bar: 1 }).hello(); 
    } 
} 

class Foo 
{ 
    public function new(options:{ bar:Int }) {} 

    public function hello() 
    { 
     trace("Hi there"); 
    } 
} 

class Fluent 
{ 
    private var parent:Null<Fluent>; 

    public function new(parent:Null<Fluent>) 
    { 
     this.parent = parent; 
    } 

    public function end() 
    { 
     if(parent != null) { 
      return parent; 
     } 

     throw 'Top level already reached'; 
    } 
} 

class FluentFoo extends Fluent 
{ 
    public var base:Foo; 

    public function new(parent:Null<Fluent>, options:{ bar:Int }) 
    { 
     super(parent); 

     base = new Foo(options); 
    } 

    public function hello() 
    { 
     base.hello(); 

     return this; 
    } 
} 

Je veux générer des classes telles que FluentFoo automatiquement.

Dans le code de pseudohaxe:

import haxe.Constraints.Constructible; 

class Test 
{ 
    public static function main() 
    { 
     var foo = new Fluent<Foo>(null, { bar: 1 }).hello(); 
    } 
} 

class Foo 
{ 
    public function new(options:{ bar:Int }) {} 

    public function hello() 
    { 
     trace("Hi there"); 
    } 
} 

@:generic 
@:genericBuild(FluentMacro.build()) 
class Fluent<T:Constructible<Dynamic -> Void>> 
{ 
    private var parent:Null<Fluent>; 
    private var base:T; 

    public function new(parent:Null<Fluent>, options:Dynamic) 
    { 
     this.parent = parent; 
     this.base = new T(options); 
    } 

    public function end() 
    { 
     if(parent != null) { 
      return parent; 
     } 

     throw 'Top level already reached'; 
    } 
} 

class FluentMacro 
{ 
    public static function build() 
    { 
     //Get "T" public methods 
     //Add them to class calling this genericBuild method (in this example to Fluent_Foo) 
     //Modify them so they return "this" 
    } 
} 

Je sais que je ne peux pas utiliser @:build comme tout ce que je voudrais obtenir de Context.getLocalType serait TInst(Fluent,[TInst(Fluent.T,[])]).

Cependant, je ne suis pas tout à fait comprendre manuel haxe sur builds générique - ils sont sous la même rubrique « macros de construction de type » comme @:build normale, mais la méthode build devrait revenir ComplexType, et non un tableau de champs. Est-il possible d'ajouter des champs dans @:genericBuild?

Merci

Répondre