[haxe][macro] A factory design using macros

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[haxe][macro] A factory design using macros

Renaud Bardet-2
Hi list !

I kicked into marcos in order to upgrade my current Factory implementation,
as for now the type construction involves Type.createInstance I regularly fail at runtime because of the parameters I feed doesn't match the constructor.

I figured out t would be a good job for macros but I don't quite understand half of the macro API.

So far I came out with this :

@:macro public static function create<T>( clas : ExprRequire<Class<T>>, args : ExprRequire<Array<Dynamic>> ) : Expr {
        
 // check first Expr is constant Type
 switch( clas.expr ) { case EConst(c): switch( c ) { case CType(t):
  // retreive contructor args
  var type = Context.getType( t ) ;
  switch( type ) { case TType( t, params ):
   // get array of parameter types from "args"
   
   // check they are matching "params"

   // return instanciated object

   default: }

  default: } default: }

 Context.error("argument number doesn't match Type's constructor", args.pos);
 return null;
}

althought I'm not sure the existing bits are correct I miss 1 important point wich relate to "get array of parameter types from 'args'",
I don't understand what Expr I need to parse in ordrer to get the content of an array.
I guess EArray(e1, e2) refers to the "e1[e2]" expression for accessing arrays so it's not relevant for me,
I thought something like this would do :

switch( args.expr ) { case EConst(c): switch( c ) { case CType(t) :
  if( t == "Array" )

but then I don't know how to parse the content of the array.

I'm only begining to understand the true power of macros and this simple case is a test run to see if I'm clever enought to use macros in more complicated ways.

Cheers,

Renaud

--
haXe - an open source web programming language
http://haxe.org