--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.
var anyClass = macro class AnyClass
{
public static var x : Int = 13;
// public static var initialFunctionLinkPackageStructure : Map<String,NestedPackages> = ${ Context.parse( FunctionLock.tempPackageStructure.toString(), Context.currentPos() ) };
};
var importExpr : ImportExpr =
{
// mode:ImportMode
mode : ImportMode.INormal,
// path:Array<{pos:Position, name:String}>
path : [ { pos : Context.currentPos(), name: "package01" },
{ pos : Context.currentPos(), name: "package02" },
{ pos : Context.currentPos(), name: "AnyOtherClass" }
]
} //var importExpr : ImportExpr =
Context.defineModule( "package01.package02", [ anyClass ], [ importExpr ] );
} // public static function main() : Void
Context.currentPos() is probably one mistake, isn't it? But how can I get the right position of an expression if the class does not yet exist?
Hello Juraj,
may I ask you for help again?
I have tried to simplify your approach. Just to understand, what happens (or what doesn't).
But anyhow it does not work.
How can I construct a new class from scratch?
var anyClass = macro class AnyClass
{
public static var x : Int = 13;
// public static var initialFunctionLinkPackageStructure : Map<String,NestedPackages> = ${ Context.parse( FunctionLock.tempPackageStructure.toString(), Context.currentPos() ) };
};
var importExpr : ImportExpr =
{
// mode:ImportMode
mode : ImportMode.INormal,
// path:Array<{pos:Position, name:String}>
path : [ { pos : Context.currentPos(), name: "package01" },
{ pos : Context.currentPos(), name: "package02" },
{ pos : Context.currentPos(), name: "AnyOtherClass" }
]
} //var importExpr : ImportExpr =
Context.defineModule( "package01.package02", [ anyClass ], [ importExpr ] );
} // public static function main() : Void
I also have tried this without to define a module but just to define a class. Either way it didn't work.
Context.currentPos() is probably one mistake, isn't it? But how can I get the right position of an expression if the class does not yet exist?
Or did I accidentally skip a step?
Can I test if the initialization has worked right after the line with Context.defineModule() within the callback?
compile.hxml --> --macro Main.compilerDirector()class Main {#if macro
compilerDirector() {Context.onAfterTyping( onAfterTypingCallback )onAfterTypingCallback( moduleTypes : Array<ModuleType> ) {
}... iterates all moduleTypes
... checks if "package01.package02.AnyClass" actually existsif not:... creates a module viaContext.defineModule( "package01.package02.AnyClass", [ newClassMacro ], [ importExpr ] );
... attaches a meta tag 'keep'
newClassMacro.meta =
[
{ name : "keep", // I have tried ":keep" || "@:keep" as well
// pos : ( macro null ).pos
pos : ( macro null ).pos
} ]
src/Main.hx:140: characters 32-41 : Type not found : package01.package02.AnyClass
package ;
import extensions.BaseClass;
import haxe.Serializer;
#if macro
import haxe.macro.Context;
import haxe.macro.Type.ModuleType;
import haxe.macro.TypeTools;
import haxe.macro.Type;
import haxe.macro.Expr.ImportExpr;
import haxe.macro.Expr;
//#else
//import package01.package02.AnyClass;
#end
class Main
{
#if macro
public static function compilerDirector() : Void
{
Context.onAfterTyping( onAfterTypingCallback );
// Context.onTypeNotFound( onTypeNotFoundCallback );
} // public static function compilerDirector() : Void
public static function onAfterTypingCallback( moduleTypes : Array<ModuleType> ) : Void
{
trace( "onAfterTypingCallback()" );
trace( "moduleTypes.length: " + moduleTypes.length );
var newClassExists : Bool = false;
for( moduleType in moduleTypes )
{
switch( moduleType )
{
case ModuleType.TClassDecl( clPath ) :
{
var classType : ClassType = clPath.get();
trace( classType.module );
if( classType.module == "package01.package02.AnyClass" )
{
trace( "package01.package02.AnyClass was found." );
newClassExists = true;
} // if( clPath == "package01.package02.AnyClass" )
} // case ModuleType.TClassDecl( clPath ) :
default :
{}
} // switch( moduleType )
} // for( moduleType in moduleTypes )
trace( "" );
if( !newClassExists )
{
var newClassMacro = macro class AnyClass
{
public static var x : String = "Great!";
};
newClassMacro.meta = [
{ name : "keep",
// pos : ( macro null ).pos
pos : ( macro null ).pos
}
];
trace( "newClassMacro.meta: " + newClassMacro.meta );
var importExpr : ImportExpr =
{
// mode:ImportMode
mode : ImportMode.INormal,
// path:Array<{pos:Position, name:String}>
path : [ { pos : ( macro null ).pos, name: "package01" },
{ pos : ( macro null ).pos, name: "package02" },
{ pos :( macro null ).pos, name: "AnotherClass" }
]
} //var importExpr : ImportExpr =
Context.defineModule( "package01.package02.AnyClass", [ newClassMacro ], [ importExpr ] );
trace( "::::::::::::::::: " + Context.getType( "package01.package02.AnyClass" ) + " :::::::::::::::::" );// ... shows that the class is known shortly after creation.} // if( !newClassExists )
} // public static function onAfterTypingCallback( moduleTypes : Array<ModuleType> ) : Void
#else
static function main()
{
trace( "AnyClass.x: " + package01.package02.AnyClass.x );
// The recently created class is not known anymore.
} // static function main()
#end
} // class Test