COMMUNITY

How to use ExprTools.getValue from extern class field?

how to use ExprTools.getValue from extern class field?

 extern class Vec2{

static var ZERO : Vec2;

}

 class Main{

    public var vec2:Vec2=Vec2.ZERO;
}


//and my macro class

macro public static function buildFields():Array<Field> {
		var fields:Array<Field> = Context.getBuildFields();

	for (f in fields) {
			
				switch (f.kind) {
					case FVar(t, e):
						if (e != null) {
		
							switch (e.expr) {
								case EField(e3, f3):
       var dd=ExprTools.getValue(e);//not support ;
//or
   //     var dd=ExprTools.getValue(e3);//not support;

	var objx:ObjectField = {field: ${f.name}, expr: macro {"default": $v{dd}}};
                                              ........

           return fields;

}

From the documentation of getValue:

  Supported expressions are:

   - `Int`, `Float` and `String` literals
   - identifiers `true`, `false` and `null`
   - structure declarations if all their fields are values
   - array declarations if all their elements are values
   - unary operators `-`, `!` and `~` if the operand is a value
   - binary operators except `=>`, `...` and assignments

  Parentheses, metadata and the `untyped` keyword are ignored.

  If any non-value is encountered, an exception of type `String` is
  thrown.

  If `e` is null, the result is unspecified.

Which do not match your public var vec2:Vec2=Vec2.ZERO; so it’s expected that it fails.

so it’s expected that it fails.

but how to do the same thing? is there any API?

How do you do what?

I mean gen code like

 vector2 : { "default" : cc.Vec2.ZERO, type : cc.Vec2}
var objx:ObjectField = {field: ${f.name}, expr: macro {"default":  $p{[$i{ss},dd]}, type:  $i{ss}}};

fixed.