1

Looking at How to define a function which accept both Seq[T] and ParSeq[T] as parameter?, I figured lub (Least Upper Bound) was appropriate

However it gives the following, long output:

import scala.reflect.runtime.universe._
scala> lub( List( typeOf[scala.collection.Seq[Int]],
                  typeOf[scala.collection.parallel.ParSeq[Int]] ) )
res1: reflect.runtime.universe.Type = scala.collection.GenSeq[Int]{def ...

It looks like GenSeq is the lub. However, how can I get the type alone, i.e. GenSeq[Int], excluding other details?

Community
  • 1
  • 1
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384

1 Answers1

1

The Type you get by calling lub is the actual LUB (Least Upper Bound), asking for the "type alone" is misleading, that one is already the correct LUB.

GenSeq[Int] is an upper bound but not the LUB, it is a superclass of the LUB.

That cryptic type is actually a RefinedType so you can obtain the list of its superclasses:

import reflect.runtime.universe._

def unRefined(tp: Type): List[Type] = tp match {
  case rtp: RefinedType => rtp.parents
  case _ => ???
}

Going back to your example, it gives you:

scala> unRefined(res1)
res2: List[reflect.runtime.universe.Type] = List(scala.collection.GenSeq[Int])
Federico Pellegatta
  • 3,977
  • 1
  • 17
  • 29