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.

@:value({ isStatic : false })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:

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,[])

@:value({ once : false })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,[])])

staticgetClass(t:Type):ClassType

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):EnumType

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.

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.

statictoString(t:Type):String

Available on macro

Converts type t to a human-readable String representation.

staticinlineunify(t1:Type, t2:Type):Bool

Available on macro

Returns true if t1 and t2 unify, false otherwise.