milessabin / shapeless

Generic programming for Scala

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

BigInt and other j.l.Numbers are considered AnyVals instead of AnyRefs with Typeable

sjrd opened this issue · comments

The following test case fails:

    val n: Any = BigInt(23)
    val cn = n.cast[AnyRef]
    assertTrue(cn.isDefined)

    val cn2 = n.cast[AnyVal]
    assertTrue(cn2.isEmpty)

This is because Typeable.isValClass uses classOf[j.l.Number].isAssignableFrom(clazz) instead of explicitly listing the 6 boxed number classes:

def isValClass[T](clazz: Class[T]) =
(classOf[jl.Number] isAssignableFrom clazz) ||
clazz == classOf[jl.Boolean] ||
clazz == classOf[jl.Character] ||
clazz == classOf[runtime.BoxedUnit]

java.math.BigInteger, scala.math.BigInt and their decimal counterparts are examples of AnyRef types that are subclasses of java.lang.Number.