This class provides some utility methods to work with types. It is best used through 'using haxe.macro.TypeTools' syntax and then provides additional methods on haxe.macro.Type instances.
Static methods
staticapplyTypeParameters(t:Type, typeParameters:Array<TypeParameter>, concreteTypes:Array<Type>):Type
Available on macro
Applies the type parameters typeParameters
to type t
with the given
types concreteTypes
.
This function replaces occurrences of type parameters in t
if they are
part of typeParameters
. The array index of such a type parameter is
then used to lookup the concrete type in concreteTypes
.
If typeParameters.length
is not equal to concreteTypes.length
, an
exception of type String
is thrown.
If typeParameters.length
is 0, t
is returned unchanged.
If either argument is null
, the result is unspecified.
staticfindField(c:ClassType, name:String, isStatic:Bool = false):Null<ClassField>
Resolves the field named name
on class c
.
If isStatic
is true, the classes' static fields are checked. Otherwise
the classes' member fields are checked.
If the field is found, it is returned. Otherwise if c
has a super
class, findField
recursively checks that super class. Otherwise null
is returned.
If any argument is null, the result is unspecified.
staticinlinefollow(t:Type, ?once:Bool):Type
Available on macro
Follows all typedefs of t
to reach the actual type.
If once
is true, this function does not call itself recursively,
otherwise it does. This can be useful in cases where intermediate
typedefs might be of interest.
Affected types are monomorphs TMono
and typedefs TType(t,pl)
.
If t
is null, an internal exception is thrown.
Usage example with monomorphs:
var t = Context.typeof(macro null); // TMono(<mono>)
var ts = Context.typeof(macro "foo"); //TInst(String,[])
Context.unify(t, ts);
trace(t); // TMono(<mono>)
trace(t.follow()); //TInst(String,[])
Usage example with typedefs:
var t = Context.typeof(macro ("foo" :MyString)); // typedef MyString = String
trace(t); // TType(MyString,[])
trace(t.follow()); //TInst(String,[])
staticinlinefollowWithAbstracts(t:Type, once:Bool = false):Type
Available on macro
Like follow
, follows all typedefs of t
to reach the actual type.
Will however follow also abstracts to their underlying implementation, if they are not a @:coreType abstract
If t
is null, an internal exception is thrown.
Usage example:
var t = Context.typeof(macro new Map<String, String>());
trace(t); // TAbstract(Map,[TInst(String,[]),TInst(String,[])])
trace(t.followWithAbstracts()); // TInst(haxe.ds.StringMap, [TInst(String,[])])
staticfromModuleType(mt:ModuleType):Type
Available on macro
Creates a type from the haxe.macro.Type.ModuleType
argument.
staticgetClass(t:Type):Null<{superClass:Null<{t:Ref<ClassType>, params:Array<Type>}>, statics:Ref<Array<ClassField>>, pos:Position, params:Array<TypeParameter>, pack:Array<String>, overrides:Array<Ref<ClassField>>, name:String, module:String, meta:MetaAccess, kind:ClassKind, isPrivate:Bool, isInterface:Bool, isFinal:Bool, isExtern:Bool, isAbstract:Bool, interfaces:Array<{t:Ref<ClassType>, params:Array<Type>}>, init:Null<TypedExpr>, fields:Ref<Array<ClassField>>, exclude:() ‑> Void, doc:Null<String>, constructor:Null<Ref<ClassField>>}>
Available on macro
Tries to extract the class instance stored inside t
.
If t
is a class instance TInst(c,pl)
, c is returned.
If t
is of a different type, an exception of type String is thrown.
If t
is null, the result is null.
staticgetEnum(t:Type):Null<{pos:Position, params:Array<TypeParameter>, pack:Array<String>, names:Array<String>, name:String, module:String, meta:MetaAccess, isPrivate:Bool, isExtern:Bool, exclude:() ‑> Void, doc:Null<String>, constructs:Map<String, EnumField>}>
Available on macro
Tries to extract the enum instance stored inside t
.
If t
is an enum instance TEnum(e,pl)
, e is returned.
If t
is of a different type, an exception of type String is thrown.
If t
is null, the result is null.
staticiter(t:Type, f:Type ‑> Void):Void
Available on macro
Calls function f
on each component of type t
.
If t
is not a compound type, this operation has no effect.
The following types are considered compound:
- TInst, TEnum, TType and TAbstract with type parameters
- TFun
- TAnonymous
If t
or f
are null, the result is unspecified.
staticmap(t:Type, f:Type ‑> Type):Type
Available on macro
Transforms t
by calling f
on each of its subtypes.
If t
is a compound type, f
is called on each of its components.
Otherwise t
is returned unchanged.
The following types are considered compound:
- TInst, TEnum, TType and TAbstract with type parameters
- TFun
- TAnonymous
If t
or f
are null, the result is unspecified.
staticsetVarName(t:TVar, name:String):Void
Available on macro
Changes the name of the variable in the typed expression.
statictoComplexType(type:Null<Type>):Null<ComplexType>
Returns a syntax-level type corresponding to Type t
.
This function is mostly inverse to ComplexTypeTools.toType
, but may
lose some information on types that do not have a corresponding syntax
version, such as monomorphs. In these cases, the result is null.
If t
is null, an internal exception is thrown.
statictoModuleType(t:Type):ModuleType
Available on macro
Converts type t
to haxe.macro.Type.ModuleType
.