Scala - Int 到 Numeric[Int] 的隐式转换

发布于 2024-09-29 17:30:34 字数 786 浏览 5 评论 0原文

我创建了一个类,可以通过任何可以转换为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋千易 2024-10-06 17:30:34

你用错了。正确的用法是这样的:

class Complex[T](val real : T, val imag : T)(implicit num: Numeric[T]) {
   import num._ // make implicit conversions available
   //... complex number methods ...
}

它与 OrderedOrdering 之间的区别相同。 Ordered[T] 实例可以与 T 进行比较,而 Ordering[T] 提供了一种比较一对 T。

You are using it wrong. The correct usage is like this:

class Complex[T](val real : T, val imag : T)(implicit num: Numeric[T]) {
   import num._ // make implicit conversions available
   //... complex number methods ...
}

It is the same difference as in between Ordered and Ordering. An Ordered[T] instance can be compared to T, while an Ordering[T] provides a method that compares a a couple of T.

擦肩而过的背影 2024-10-06 17:30:34

在 Scala 2.8 中,它也可以写成

class Complex[T: Numeric] (val real : T, val imag : T) {

  def +(that: Complex[T]) = {
    val r = implicitly[Numeric[T]].plus(this.real, that.real)
    val i = implicitly[Numeric[T]].plus(this.imag, that.imag)
    new Complex(r, i)
  }

}

这种语法确实有点密集,但可以像这样变得更具可读性:

class Complex[T: Numeric] (val real : T, val imag : T) {
  val num = implicitly[Numeric[T]]

  def +(that: Complex[T]) = {
    new Complex(num.plus(this.real, that.real), num.plus(this.imag, that.imag))
  }

}

声明 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

class Complex[T: Numeric] (val real : T, val imag : T) {

  def +(that: Complex[T]) = {
    val r = implicitly[Numeric[T]].plus(this.real, that.real)
    val i = implicitly[Numeric[T]].plus(this.imag, that.imag)
    new Complex(r, i)
  }

}

This syntax is admittedly a bit dense, but it can be made more readable like this:

class Complex[T: Numeric] (val real : T, val imag : T) {
  val num = implicitly[Numeric[T]]

  def +(that: Complex[T]) = {
    new Complex(num.plus(this.real, that.real), num.plus(this.imag, that.imag))
  }

}

The declaration class C[T: M]( ... ) { val x = implicitly[M[T]] would seem to be equivalent to class 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 case x is a method, and in the second case it's a field.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文