70. A.2
val l1 = List(1, 2, 3)
val l2 = List(1.5, 2.5, 3.5)
l1.sum
l2.sum
// def sum [B >: A] (implicit num: Numeric[B]): B
l1.sum[Int](?:Numeric[Int])
l2.sum[Double](?:Numeric[Double])
71. package scala.math
...
object Numeric {
trait IntIsIntegral extends Integral[Int] {
def plus(x: Int, y: Int): Int = x + y
def minus(x: Int, y: Int): Int = x - y
def times(x: Int, y: Int): Int = x * y
def quot(x: Int, y: Int): Int = x / y
def rem(x: Int, y: Int): Int = x % y
def negate(x: Int): Int = -x
def fromInt(x: Int): Int = x
def toInt(x: Int): Int = x.toInt
def toLong(x: Int): Long = x
def toFloat(x: Int): Float = x
def toDouble(x: Int): Double = x
}
..
// Integeral[T] <: Numeric[T] なので、 Numeric[Int]のimplicitな値も同時に定義
// していることになる
implicit object IntIsIntegral extends IntIsIntegral with
Ordering.IntOrdering
}
72. A.3
val l1 = List("A" -> 1, "B" -> 2)
val l2 = List(1, 2, 3)
l1.toMap
l2.toMap 名状し難い型
l1.toMap[String, Int](?: (String, Int) <:< (String, Int))
l2.toMap[B1, B2](?:B3)
→ error: Cannot prove that Int <:< (T, U).
74. package scala.math
...
object Predef ... {
@implicitNotFound(msg = "Cannot prove that ${From} <:< ${To}.")
sealed abstract class <:<[-From, +To] extends (From => To) with
Serializable 名状し難い型の定義
private[this] final val singleton_<:< = new <:<[Any,Any] {
def apply(x: Any): Any = x }
// not in the <:< companion object because it is also
// intended to subsume identity (which is no longer implicit)
implicit def conforms[A]: A <:< A = singleton_<:<.asInstanceOf[A <:< A]
}