TypeTools.applyTypeParameters - how it work?

Guys, I just can’t figure out how to use the haxe.macro.TypeTools.applyTypeParameters method. Search the forum found no results. Maybe someone has any example of how this works?

What are you trying to do, can you share some code?

The method replace the type parameters (2nd argument) with the concrete types (3rd argument) in the type using those (1st argument).

There’s not really any trick to it, but you do need to replace the correct type parameters, and with the correct concrete type.

1 Like

In fact, I’m not trying to do anything, because I do not understand what I can do with it.
Here is an example. After using applyTypeParameters, I’m not getting any changes.

You use it when you manipulate types that have type parameters, and you want to use it outside of the type, so where the type parameter don’t exist and you can’t refer to it. That’s why you have to apply it.

Not really sure why it doesn’t work on your example, do you’d use it somewhat like this:

function bake(type:Type):Type {
  return switch (type) {
    case TInst(_.get() => t, p):
      type.applyTypeParameters(t.params, p);

if you wanted to “bake” the type parameters.

1 Like

I still can not understand. I did what you said, but it has no effect.
Initially, I thought I could get a ClassType instance where all monomorphs are replaced by concrete types. But now I don’t understand anything at all :smile: Does this method work exactly?

There is no effect to be done in that second example, the expected type already has it concrete value.
It should be more useful on the type of fields, like var a : Array<T> to remove that T.

But yes this function does work, I have used it successfully, you can check a real exemple json2object/DataBuilder.hx at a06453b3a0541eea0eda0e19b160771846f9edf6 · elnabo/json2object · GitHub but be warned that it’s not going to be an easy read, even for me :wink:

1 Like

Thanks, thanks to you, I figured it out and it turned out really simple. I got confused because I tried to use this method as in the first example, but apparently it should not be used in this way.