jeudi 2 janvier 2020

How to get underlying constant type from singleton type with Scala reflection API

import scala.reflect.runtime.universe._

val a: 42 = 42
val t: Type = typeOf[a.type]
assert(getConstantType(t).get =:= typeOf[42])

def getConstantType(t: Type): Option[ConstantType] = ???

How could I generally implement getConstantType so that the above assertion passes? I assumed that something like this was possible since the assertion below passes:

assert(t <:< typeOf[42])

t.widen goes too far as it return Int. I'm looking for something that returns Int(42).





Aucun commentaire:

Enregistrer un commentaire