# How to convert haxe.macro.Type to Expr?

**URL:** <https://community.haxe.org/t/how-to-convert-haxe-macro-type-to-expr/4313>\
**Category:** Haxe\
**Created:** [August 19, 2024, 1:39pm UTC](https://community.haxe.org/t/how-to-convert-haxe-macro-type-to-expr/4313 "2024-08-19T13:39:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![barisyild](https://community.haxe.org/user_avatar/community.haxe.org/barisyild/32/1376_2.png) [@barisyild](https://community.haxe.org/u/barisyild)\
**Post date:** [August 19, 2024, 1:39pm UTC](https://community.haxe.org/t/how-to-convert-haxe-macro-type-to-expr/4313/1 "2024-08-19T13:39:36Z")

</div>

```haxe
var clazz = Context.getType("SampleClass");
var expr = macro $clazz;

```

haxe.macro.Type should be haxe.macro.Expr

```haxe
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.

---

<div class="post-metadata">

**Author:** ![0b1kn00b](https://community.haxe.org/user_avatar/community.haxe.org/0b1kn00b/32/1170_2.png) [@0b1kn00b](https://community.haxe.org/u/0b1kn00b)\
**Post date:** [August 19, 2024, 7:53pm UTC](https://community.haxe.org/t/how-to-convert-haxe-macro-type-to-expr/4313/2 "2024-08-19T19:53:08Z")

</div>

`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`.

> **[haxe.macro.TypeTools - Haxe 4.3.5 API](https://api.haxe.org/haxe/macro/TypeTools.html#toComplexType)**
>
> 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.

and then slot your complex type where it belongs:

> <https://github.com/HaxeFoundation/haxe/blob/4.3.5/std/haxe/macro/Expr.hx>

which will be as an annotation or a cast.

---

<div class="post-metadata">

**Author:** ![0b1kn00b](https://community.haxe.org/user_avatar/community.haxe.org/0b1kn00b/32/1170_2.png) [@0b1kn00b](https://community.haxe.org/u/0b1kn00b)\
**Post date:** [August 19, 2024, 8:04pm UTC](https://community.haxe.org/t/how-to-convert-haxe-macro-type-to-expr/4313/3 "2024-08-19T20:04:20Z")

</div>

If you want a class directly, you’ll need to make a `haxe.macro.TypePath` and plug it into `ENew`

> <https://github.com/HaxeFoundation/haxe/blob/bd79571b89d719a45db7860d239da2164147dd15/std/haxe/macro/Expr.hx#L475>

---

<div class="post-metadata">

**Author:** ![barisyild](https://community.haxe.org/user_avatar/community.haxe.org/barisyild/32/1376_2.png) [@barisyild](https://community.haxe.org/u/barisyild)\
**Post date:** [August 19, 2024, 10:42pm UTC](https://community.haxe.org/t/how-to-convert-haxe-macro-type-to-expr/4313/4 "2024-08-19T22:42:42Z")

</div>

```haxe
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”.

---

<div class="post-metadata">

**Author:** ![back2dos](https://community.haxe.org/user_avatar/community.haxe.org/back2dos/32/30_2.png) [@back2dos](https://community.haxe.org/u/back2dos)\
**Post date:** [August 20, 2024, 3:05am UTC](https://community.haxe.org/t/how-to-convert-haxe-macro-type-to-expr/4313/5 "2024-08-20T03:05:34Z")

</div>

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`](https://haxe.org/manual/macro-reification-expression.html), i.e. `macro $p{parts}` where `parts` is an `Array<String>` of the individual path fragments. Example: [Try Haxe!](https://try.haxe.org/#bF46AA3B)

---

<div class="post-metadata">

**Author:** ![barisyild](https://community.haxe.org/user_avatar/community.haxe.org/barisyild/32/1376_2.png) [@barisyild](https://community.haxe.org/u/barisyild)\
**Post date:** [August 24, 2024, 9:58pm UTC](https://community.haxe.org/t/how-to-convert-haxe-macro-type-to-expr/4313/6 "2024-08-24T21:58:39Z")

</div>

I am trying to add classes to a map.
