5
Welcome to Scala version 2.10.2 (Java HotSpot 64-Bit Server VM, Java 1.7.0_15).

scala> :paste
// Entering paste mode (ctrl-D to finish)

trait Reduce { type X; def add(x:X) }

我现在声明一个类, ,它可以从一个,或一个Foo填充。Reducerchain

class Foo[A](val a:A) {
  def fill(r: Reduce { type X = A}) = {r.add(a)}
  def chain[R >:A](r: Reduce { type X = R }) = { r.add(a); new Foo(r)}
}

我现在创建一个类,它是一些数字类型的化简器Y

class AsInt[Y: Numeric] extends Reduce { 
  type X = Y
  var i = 0
  override def add(y:Y) = {i = implicitly[Numeric[Y]].toInt(y)}
}

我已经完成了

// Exiting paste mode, now interpreting.

defined trait Reduce
defined class Foo
defined class AsInt

我现在可以创建并填充以下实例Foo

scala> val fL = new Foo(123L)
fL: Foo[Long] = Foo@12979ef0

scala> fL.fill(new AsInt)

到目前为止,一切都很好。现在我链接一个:

scala> fL.chain(new AsInt)
<console>:12: error: ambiguous implicit values:
 both object BigIntIsIntegral in object Numeric of type scala.math.Numeric.BigIntIsIntegral.type
 and object IntIsIntegral in object Numeric of type scala.math.Numeric.IntIsIntegral.type
 match expected type Numeric[Y]
              fL.chain(new AsInt)
                       ^

现在我被困住了。打字机应该寻找某种类型R >: Long,它Numeric[R]在范围内是隐含的。aNumeric[Int]或 a怎么Numeric[BigInt]可能符合要求?

打字机似乎没有解决它的问题:

scala> def foo[Z >: Long](implicit N: Numeric[Z]) = println(N)
foo: [Z >: Long](implicit N: Numeric[Z])Unit

scala> foo
scala.math.Numeric$LongIsIntegral$@67236f24

我错过了什么?

4

0 回答 0