macros and "using" keyword

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

macros and "using" keyword

clemos
Hi list,

I've been playing with macros, and have been trying to use them
together with "using", to do something like this :

/////////////////
class Model {
  public var id : Int;
  public var name : String;
  public var content : String;
}
//////////////////
class ModelTools {
  @:macro public static function set( model : haxe.macro.Expr , values
: haxe.macro.Expr ){
     //  here typecheck values against model definition and generate
code to set model fields
  }
}
////////////////
using ModelTools

var m = new Model();
m.set({ id : 1 , name : "foo" }); // fine
m.set({ id : 2 , name : "bar" , content : "hello world" }); // fine
m.set({ bar : "notDefinedInModel" }); // throw compile time error
///////////////

This kind of code actually compiles fine, but unfortunately the macro
doesn't get executed.
To me, this should at least trigger a compile time error, since the
generated code will produce obvious runtime errors (ModelTools.set is
undefined).
This being said, I'd like to see this working :)
Maybe I'm wrong, but it seems extremely powerful to me to be able to
use macros and "using" together.

What do you think ?
Is there a bug here ?
Would it be an improvement to have such feature ?
Are there other ways to make it work ?

Cheers,
Clément

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: macros and "using" keyword

Heinz Hölzer-2
I would love to see a combination of macros and using.
I posted an idea for this here:

http://haxe.1354130.n2.nabble.com/Some-Macro-Questions-tp5860419p5888080.html

Am 12.02.2011 18:35, schrieb clemos:

> Hi list,
>
> I've been playing with macros, and have been trying to use them
> together with "using", to do something like this :
>
> /////////////////
> class Model {
>    public var id : Int;
>    public var name : String;
>    public var content : String;
> }
> //////////////////
> class ModelTools {
>    @:macro public static function set( model : haxe.macro.Expr , values
> : haxe.macro.Expr ){
>       //  here typecheck values against model definition and generate
> code to set model fields
>    }
> }
> ////////////////
> using ModelTools
>
> var m = new Model();
> m.set({ id : 1 , name : "foo" }); // fine
> m.set({ id : 2 , name : "bar" , content : "hello world" }); // fine
> m.set({ bar : "notDefinedInModel" }); // throw compile time error
> ///////////////
>
> This kind of code actually compiles fine, but unfortunately the macro
> doesn't get executed.
> To me, this should at least trigger a compile time error, since the
> generated code will produce obvious runtime errors (ModelTools.set is
> undefined).
> This being said, I'd like to see this working :)
> Maybe I'm wrong, but it seems extremely powerful to me to be able to
> use macros and "using" together.
>
> What do you think ?
> Is there a bug here ?
> Would it be an improvement to have such feature ?
> Are there other ways to make it work ?
>
> Cheers,
> Clément
>


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