简体   繁体   English

在Scala中对泛型类实现二进制操作

[英]Implementing binary operations on generic class in scala

I am having multiple compile errors trying to write a class definition like this. 我在尝试编写这样的类定义时遇到多个编译错误。



    trait BinOps[T] {
      def +(that: Vector[T]): Vector[T]
      def -(that: Vector[T]): Vector[T]
      def *(that: Vector[T]): Vector[T]
      def /(that: Vector[T]): Vector[T]
    }

    trait Vector[T] {
      def toList(): List[T]
      def zeros(length: Int): Vector[T]
    }

    object Vector {
      def apply[T](args: T*): Vector[T] = new VectorImpl[T](args.toList)

      private class VectorImpl[@specialized(Double, Int, Float, Long)T](val _data: List[T])
        extends Vector[T]
        with BinOps[T] {
        def +(that: Vector[T]): Vector[T] = new VectorImpl[T](_data.zip(that).map(elem => elem._1 + elem._2))
        def -(that: Vector[T]): Vector[T] = new VectorImpl[T](_data.zip(that).map(elem => elem._1 - elem._2))
        def *(that: Vector[T]): Vector[T] = new VectorImpl[T](_data.zip(that).map(elem => elem._1 * elem._2))
        def /(that: Vector[T]): Vector[T] = new VectorImpl[T](_data.zip(that).map(elem => elem._1 / elem._2))

        def fill(length: Int, value: T): Vector[T] = new VectorImpl[T](List.fill[T](length)(value))
        def toList(): List[T] = _data.toList
      }

      implicit def VectorToList[T](v: Vector[T]): List[T] = v.toList
    }

I am getting errors like these.. 我收到这样的错误。


    Error:(36, 102) type mismatch;
     found   : T
     required: String
        def +(that: Vector[T]): Vector[T] = new VectorImpl[T](_data.zip(that).map(elem => elem._1 + elem._2))
                                                                         ^   
    Error:(37, 95) value - is not a member of type parameter T
        def -(that: Vector[T]): Vector[T] = new VectorImpl[T](_data.zip(that).map(elem => elem._1 - elem._2))
                                                                         ^

However doing a zip should have resulted in a tuple with type T and I am trying to access the first and second elements. 但是,执行zip应该会导致产生一个T型元组,而我正在尝试访问第一个和第二个元素。 So what am I doing wrong ? 那么我在做什么错呢?

Error:(37, 95) value - is not a member of type parameter T 错误:(37,95)值-不是类型参数T的成员

This is the real error (the one about strings is an unfortunate consequence of an implicit conversion in Predef - I recommend building with -Yno-predef to avoid such misleading errors). 这是真正的错误(关于字符串的错误是Predef隐式转换的不幸结果-我建议使用-Yno-predef构建以避免此类误导性错误)。 Your T is unconstrained - what is - supposed to do? 您的T不受限制-应该-应该做什么? Eg what if T was Locale - what is Locale.ENGLISH - Locale.SPANISH ? 例如,如果TLocale ,怎么Locale.ENGLISH - Locale.SPANISH什么? So it doesn't compile. 因此它不会编译。

All the answers above to point to the right problem. 以上所有答案均指向正确的问题。 I will post the answer of what finally worked. 我将发布最终可行的答案。 As suggested by JimN and from help from the scala irc this worked for me finally. 正如JimN所建议的,并在scala irc的帮助下,这最终为我工作了。 Thanks a lot for the explanations 非常感谢您的解释

import Numeric.Implicits._
import Fractional.Implicits._


trait BinOps[T] {
  def +(that: Vector[T])(implicit ev: Numeric[T]): Vector[T]
  def -(that: Vector[T])(implicit ev: Numeric[T]): Vector[T]
  def *(that: Vector[T])(implicit ev: Numeric[T]): Vector[T]
  def /(that: Vector[T])(implicit ev: Fractional[T]): Vector[T]
}

trait Vector[T] {
  def toList(): List[T]
  def zeros(length: Int)(implicit ev: Numeric[T]): Vector[T]
}


object Vector {
  def apply[T](args: T*): Vector[T] = new VectorImpl[T](args.toList)

  private class VectorImpl[@specialized(Double, Int, Float, Long)T](val _data: List[T])
    extends Vector[T]
    with BinOps[T] {
    def +(that: Vector[T])(implicit ev: Numeric[T]): Vector[T] = new VectorImpl[T](_data.zip(that).map(elem => elem._1 + elem._2))
    def -(that: Vector[T])(implicit ev: Numeric[T]): Vector[T] = new VectorImpl[T](_data.zip(that).map(elem => elem._1 - elem._2))
    def *(that: Vector[T])(implicit ev: Numeric[T]): Vector[T] = new VectorImpl[T](_data.zip(that).map(elem => elem._1 * elem._2))
    def /(that: Vector[T])(implicit ev: Fractional[T]): Vector[T] = new VectorImpl[T](_data.zip(that).map(elem => elem._1 / elem._2))

    def fill(length: Int, value: T): Vector[T] = new VectorImpl[T](List.fill[T](length)(value))
    def zeros(length: Int)(implicit ev: Numeric[T]): Vector[T] = fill(length, ev.zero)
    def toList(): List[T] = _data.toList
  }

  implicit def VectorToList[T](v: Vector[T]): List[T] = v.toList
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM