Scala Compile-time checking of object using quoted.Type

Viewed 81

I have a Scala 3 class whose instances contain a quoted.Type object to capture the type of the instance:

class Skey[T](val name: Symbol)(using val qt: quoted.Type[T])

The whole point of having qt there is to verify that the Skey is of the right type, but, in a typical case, where I have a function that wants to return a retrieved Skey, represented schematically thus (edited since original post):

class SkeyIndex(...):
  private var hMap = immutable.HashMap[String, Skey[?]]:
  ...
  def lookup(y: Skey[T], ...): Option[Skey[T]] =
    val existing: Option[Skey[?]] = hMap.get(name)
    existing match 
      case None => None
      case x @ Skey[?] => 
         if x.qt == y.qt then x // THIS IS NOT QUITE RIGHT!
         else ...

the equality test verifies that x is indeed of the correct type, but at run time. Is there a quoted.Type match idiom or some magic involving match types that will allow the compiler to infer that x is an Skey[T] and not some other sort of Skey?

I'm sure there's no way to do this in Scala 2, but I have my fingers crossed for Scala 3, which has a cleaner treatment of type-level programming.

0 Answers
Related