为什么在这种情况下不考虑带有泛型参数的隐式转换?

编程入门 行业动态 更新时间:2024-10-28 20:23:04
本文介绍了为什么在这种情况下不考虑带有泛型参数的隐式转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

请考虑以下代码,这些代码来自 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.

更多推荐

为什么在这种情况下不考虑带有泛型参数的隐式转换?

本文发布于:2023-08-04 02:25:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1292032.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:在这种情况下   参数   隐式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!