0

I’m trying to write the following:

val value: Int = 3
val tpe = typeOf(value)    // This is pseudocode. What should
                           // actually go on this line?
q"""
type U = $tpe
val v: U = $value
"""

Essentially, I need to capture the type of value (Int) in tpe and assign it to U. How does one do this?

Dan Li
  • 866
  • 1
  • 7
  • 19

1 Answers1

2

Try

import scala.reflect.runtime.universe._
def getType[A: TypeTag](a: A): Type = typeOf[A]

val value: Int = 3
val tpe = getType(value) // Int

Connected question: How does one obtain the type of the value that an AST represents?

Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
  • Both of these solutions work — thank you. I’ve marked this as answered. I realized this morning, though, that I’ve asked a different question than the one I intended. In my question, `value` should be a `Tree`, not a value in itself. The correct question is here; if you could answer again there, I’d be most appreciative: https://stackoverflow.com/questions/55801743/how-does-one-obtain-the-type-that-an-ast-represents – Dan Li Apr 22 '19 at 22:12
  • @DanielLi I edited my answer. I transfered other part of my answer there. – Dmytro Mitin Apr 22 '19 at 23:37