Pattern match on erased HKT

How to extract Higher Kind Types from erased value?

It doesn’t work


type ExtractF[F[_], T] = F[T]

inline def check[X] =
  inline compiletime.erasedValue[X] match
    case _: ExtractF[tc, t] => 1
    case _ => 2

@main def main() =
  println(check[List[Int]])           //2
  println(check[Option[Int]])         //2

you can try remixing this with various combination of compiletime.summonFrom - e.g. i didnt think far enough ahead to how to do this with poly-kinds:

scala> trait HKMirror:
     |   type TC[_]
     |   type Arg
     |   type Monotype = TC[Arg]
     | object HKMirror:
     |   given [F[_], T] => (HKMirror { type TC = F; type Arg = T }) = new HKMirror { type TC = F; type Arg = T }
     |
// defined trait HKMirror
// defined object HKMirror

scala> type HK_TC[TC0[_]] = HKMirror { type TC = TC0 }
     | inline def check[X](using hkm: HKMirror { type Monotype = X }) =
     |   inline compiletime.erasedValue[hkm.type] match
     |     case _: HK_TC[tc] => 1
     |     case _ => 2
     |
// defined alias type HK_TC[TC0[_$1]] = HKMirror{type TC = TC0}
def check[X](using hkm: HKMirror{type Monotype = X}): Int

scala> def main() =
     |   println(check[List[Int]])
     |   println(check[Option[Int]])
     |
def main(): Unit

scala> main()
1
1
1 Like