请考虑以下代码,这些代码来自 metascala项目:
Consider the following code, derived from the metascala project:
object Units { case class Quantity[M <: MInt, T: Numeric](value: T) { type This = Quantity[M, T] def *[M2 <: MInt](m: Quantity[M2, T]) = Quantity[M + M2, T](numeric[T].times(value, m.value)) def /[M2 <: MInt](m: Quantity[M2, T]) = Quantity[M - M2, T](numeric[T].div(value, m.value)) def apply(v: T) = Quantity[M, T](numeric[T].times(v, value)) } implicit def measure[T: Numeric](v: T) = Quantity[_0, T](v) implicit def numericToQuantity[T: Numeric](v: T) = new QuantityConstructor[T](v) class QuantityConstructor[T: Numeric](v: T) { def m = Quantity[_1, T](v) } }(MInt基本上是Peano编号的实现,其中_0,_1是来自 metascala .告诉我是否需要其他代码,我只是不想在这里粘贴所有内容.)
(the MInt is basically the implementation of peano numbers where _0, _1 are concrete "values" from metascala. Tell me if you need additional code, I just didn't want to paste everything in here.)
我想支持这样的代码,其中一些现有的数量可以乘以一个简单的数字,例如. g.
I want to support code where some existing Quantity can be multiplied by a simple number, e. g.
import Units._ val length1 = 5 * (5 m) //doesn't work <----- val length2 = (5 m) * 5 // works为什么我假设没有在第一行代码中考虑隐式方法measure?
Why is the implicit method measure not considered in the first line of code as I have assumed?
相反,我收到此错误消息:
Instead I get this error message:
overloaded method value * with alternatives: (x: Double)Double <and> (x: Float)Float <and> (x: Long)Long <and> (x: Int)Int <and> (x: Char)Int <and> (x: Short)Int <and> (x: Byte)Int cannot be applied to (scalax.units.Units3.Quantity[scalax.units.Integers._1,Int])我正在使用Scala 2.10-trunk.
I'm using Scala 2.10-trunk.
这实际上是对"1 * BigInt(1)"如何工作,我该怎么做?.
推荐答案要开始做这件事,这是一个出现相同问题的独立示例,
To get things started, here's a stand-alone example that gives the same problem,
object Units { case class Quantity[T: Numeric](value: T) { def *[M](m: Quantity[T]) = // type M can't be inferred below Quantity[T](implicitly[Numeric[T]].times(value, m.value)) } implicit def measure[T: Numeric](v: T) = Quantity[T](v) val length0 = measure(5) * Quantity(5) // works val length1 = 5 * Quantity(5) // doesn't work }由于某种原因,由于方法*上的类型参数M,找不到转换measure.如果从*中删除了type参数,则编译正常.也许其他人可以解释为什么?
For some reason, the conversion measure isn't being found because of the type parameter M on the method *. If the type parameter is removed from *, things compile fine. Maybe someone else can explain why?
编辑.这看起来像Scala编译器的局限性,因为将*重命名为类似***的名称可以解决此问题.可能Int.*(没有类型参数)的存在排除了使用Quantity.*[M](带有类型参数)的隐式转换.这使我想起了这样的要求,即重写方法必须具有相同的确切类型参数.
Edit. This is looking like a limitation of the Scala compiler, since renaming * to something like *** resolves the problem. Perhaps the existence of Int.* (without a type parameter) is precluding the implicit conversion for use of Quantity.*[M] (with a type parameter). This reminds me of the requirement that overridden methods must have the same exact type parameters.
更多推荐
为什么在这种情况下不考虑带有泛型参数的隐式转换?
发布评论