Scala - Int 到 Numeric[Int] 的隐式转换
我创建了一个类,可以通过任何可以转换为 Numeric 的内容进行参数化
class Complex[T <% Numeric[T]] (val real : T, val imag : T) {
//... complex number methods ...
}
然后在代码的其他地方尝试:
var myComplex = new Complex(0, 1)
这会引发编译错误,因为(令人惊讶的是)Int 和 Numeric[Int] 之间甚至 Int 之间没有隐式转换和积分[Int]。
我错过了什么吗?是否有我没有看到的隐式转换?
Numeric.scala 中定义了一个名为 IntIsIntegral 的隐式对象。我尝试使用它来创建我自己的隐式转换方法:
def implicit intToNumericInt(val i : Int)(implicit n : IntIsIntegral) = n.fromInt(i)
我很惊讶这是必需的,而且无论如何,它似乎会导致 .fromInt 方法的无限递归。
我确信我错过了一些基本的东西(正如你所知,我是 Scala 的新手),所以希望能指出正确的方向。
正如您从示例中看到的,我正在尝试让复数实现正常工作,它可以接受并使用任何数字类型。我希望将其贡献给 scalala(线性代数)项目。接下来,我想介绍一个 Trait,它描述矩阵中元素的职责(主要是 + 和 * 运算符),并将对复数的支持改进到矩阵操作库中。
I've created a class that can be parameterised by anything that can be converted to Numeric
class Complex[T <% Numeric[T]] (val real : T, val imag : T) {
//... complex number methods ...
}
Then elsewhere in the code I try:
var myComplex = new Complex(0, 1)
This raises a compilation error because (surprisingly) there's no implicit conversion between Int and Numeric[Int] or even between Int and Integral[Int].
Am I missing something? Is there an implicit conversion somewhere I'm not seeing?
There's an implicit object called IntIsIntegral defined in Numeric.scala. I've tried using this to create my own implicit conversion method:
def implicit intToNumericInt(val i : Int)(implicit n : IntIsIntegral) = n.fromInt(i)
I'm surprised that this is required and, anyway, it seems to lead to an infinite recursion into the .fromInt method.
I'm sure that I'm missing something basic (as you can tell, I'm new to Scala) so would appreciate a point in the right direction.
As you can see from the example, I'm trying to get a Complex number implementation working which can accept and work with any Numeric type. I hope to contribute this to the scalala (linear algebra) project. Following that, I want to introduce a Trait which describes the responsibilities of elements in a matrix (mainly just + and * operators) and retrofit support for complex numbers into the matrix manipulation library.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你用错了。正确的用法是这样的:
它与
Ordered
和Ordering
之间的区别相同。Ordered[T]
实例可以与T
进行比较,而Ordering[T]
提供了一种比较一对T。
You are using it wrong. The correct usage is like this:
It is the same difference as in between
Ordered
andOrdering
. AnOrdered[T]
instance can be compared toT
, while anOrdering[T]
provides a method that compares a a couple ofT
.在 Scala 2.8 中,它也可以写成
这种语法确实有点密集,但可以像这样变得更具可读性:
声明
class C[T: M]( ... ) { val x = 隐式[M[T]]
似乎等同于class C[T]( ... )(implicit x: M[T]) { import x._
如中所述对先前解决方案的评论。它不仅仅是语法糖,因为它的编译方式有所不同,例如,在第一种情况下,x
是一个方法,在第二种情况下,它是一个字段。In Scala 2.8, it can also be written as
This syntax is admittedly a bit dense, but it can be made more readable like this:
The declaration
class C[T: M]( ... ) { val x = implicitly[M[T]]
would seem to be equivalent toclass C[T]( ... )(implicit x: M[T]) { import x._
as noted in the comments to the previous solution. It's not simply syntactic sugar, because there are differences in how it is compiled, e.g. in the first casex
is a method, and in the second case it's a field.