manual/macros [en]
Differences between version #15089 and #15092
129a130,168
> ===== Context API and macro Context =====
>
> The [[/api/haxe/macro/Context]] class gives you access to a lot of informations, such as compilation parameters, but also the ability to load type or even create new classes.
>
> The Context API operates on the "application context", which is the unit in which the compilation of your code occurs. There is another context which is the "macro context" which compiles and run the macro code. Please note that these two contexts are separated.
>
> For instance, if you compile a Javascript file :
> * the application context will contain your Haxe/Javascript code, it will have access to the Javascript API
> * the macro context will contain your macro code (the classes in which @:macro methods are declared) and it will not be able to access the Javascript API. It can however access the [[/api/sys]] API and the [[/api/neko]] API as well. It can still interact and modify the application context by using the [[/api/haxe/macro/Context]] class.
>
> It is important to understand that some code sometimes get compiled twice : once inside the application context and once inside the macro context.
>
> In general, it is recommended to completely separate your macro code (classes contaiting @:macro statements) from your application code, in order to prevent expected issues due to some code being included in the wrong context.
>
> ===== Type Reification =====
>
> It is possible to use ''macro : Type'' to build a type instead of an expression.
>
> The following example will declare a new typed variable when called :
>
> <code haxe>
> import haxe.macro.Expr;
> class Test {
> @:macro static function decl( vname : String ) {
> var str : ComplexType = macro : String;
> var arr : ComplexType = macro : Array<Array<$str>>;
> return macro var $vname : $arr = [];
> }
> #if !macro
> static function main() {
> decl("table");
> trace(table);
> }
> #end
> }
> </code>
>
> Please note that in that case we need to make sure that the ''main()'' code is not compiled as part of the macro context.
>
| Ver | Date | User | Action |
|---|---|---|---|
| #15796 | 2012-12-14 21:18:43 | MarcWeber | View | Diff |
| #15795 | 2012-12-14 15:00:52 | JLM | View | Diff |
| #15794 | 2012-12-14 14:58:43 | JLM | View | Diff |
| #15597 | 2012-10-28 16:07:47 | ncannasse | View | Diff |
| #15596 | 2012-10-28 16:04:09 | ncannasse | View | Diff |
| #15595 | 2012-10-28 15:53:47 | ncannasse | View | Diff |
| #15414 | 2012-08-24 23:50:52 | Confidant | View | Diff |
| #15118 | 2012-07-18 14:13:51 | Simn | View | Diff |
| #15096 | 2012-07-16 19:46:08 | JLM | View | Diff |
| #15094 | 2012-07-16 18:37:31 | ncannasse | View | Diff |
| #15092 | 2012-07-16 18:32:21 | ncannasse | View | Diff |
| #15089 | 2012-07-16 18:14:54 | ncannasse | View | Diff |
| #14425 | 2012-07-14 07:15:41 | Confidant | View | Diff |
| #14424 | 2012-07-14 06:55:36 | Confidant | View | Diff |
| #14406 | 2012-07-10 10:48:58 | ncannasse | View | Diff |
| #14363 | 2012-07-04 18:00:38 | ncannasse | View | Diff |
| #14183 | 2012-05-30 19:53:55 | Simn | View | Diff |
| #12651 | 2012-03-27 16:41:58 | Simn | View | Diff |
| #12649 | 2012-03-27 16:22:13 | ncannasse | View | Diff |
| #11950 | 2011-11-30 12:38:40 | JLM | Restored to version #10852 |
Previous | Next