var clazz = Context.getType("SampleClass");
var expr = macro $clazz;
haxe.macro.Type should be haxe.macro.Expr
var clazz = Context.toComplexType(Context.getType("SampleClass"));
var expr = macro $clazz;
Null<haxe.macro.ComplexType> should be haxe.macro.Expr
I am trying to convert Type to Class so I can write a class to a variable.
0b1kn00b
(0b1kn00b)
August 19, 2024, 7:53pm
2
ComplexType
is part of the haxe.macro.Expr
hierarchy. You can represent some, but not all haxe.macro.Type
instances via haxe.macro.TypeTools.toComplexType
.
and then slot your complex type where it belongs:
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
This file has been truncated. show original
which will be as an annotation or a cast.
0b1kn00b
(0b1kn00b)
August 19, 2024, 8:04pm
3
If you want a class directly, you’ll need to make a haxe.macro.TypePath
and plug it into ENew
EArrayDecl(values:Array<Expr>);
/**
A call `e(params)`.
**/
ECall(e:Expr, params:Array<Expr>);
/**
A constructor call `new t(params)`.
**/
ENew(t:TypePath, params:Array<Expr>);
/**
An unary operator `op` on `e`:
- `e++` (`op = OpIncrement, postFix = true`)
- `e--` (`op = OpDecrement, postFix = true`)
- `++e` (`op = OpIncrement, postFix = false`)
- `--e` (`op = OpDecrement, postFix = false`)
- `-e` (`op = OpNeg, postFix = false`)
- `!e` (`op = OpNot, postFix = false`)
var clazz = Context.toComplexType(Context.getType("SampleClass"));
var expr = macro $clazz;
The code above creates a proper ComplexType object, so there is no exception or null return, but I get the error “Null<haxe.macro.ComplexType> should be haxe.macro.Expr”.
back2dos
(Juraj Kirchheim)
August 20, 2024, 3:05am
5
If you want to reference a class object such as some.pack.TheClass
, you’ll need to construct the corresponding dot path syntax, which is an EIdent
nested in a bunch of EField
s.
The easiest way to do this is $p
, i.e. macro $p{parts}
where parts
is an Array<String>
of the individual path fragments. Example: Try Haxe!
I am trying to add classes to a map.