jilen
jilen

Reputation: 5763

scala quasiquotes convert Tree to AppliedType

I wan to infer an implicit value of an AppliedType, here's what I've done

val valueType = accessorTree.returnType
val encoderType = tq"DatumEncoder[$valueType]" // returns a Tree
val encoder = c.inferImplicitValue(encoderType) // require a Type

But the tq returns a Tree

How Can I convert it to a Type

Upvotes: 1

Views: 298

Answers (1)

Jatin
Jatin

Reputation: 31754

This link contains detailed response for tq types interpolator.

You could just do: encoderType.tpe

Or you could:

import reflect.runtime.currentMirror 
import tools.reflect.ToolBox 
val toolbox = currentMirror.mkToolBox()

def typecheckType(tree: Tree): Type = toolbox.typecheck(tree, toolbox.TYPEmode).tpe
typecheckType(encoderType)

Upvotes: 3

Related Questions