简体   繁体   English

适用于原始类型的Scala上型边界

[英]Scala Upper Type Bounds for Primitive Types

I am new to Scala and I want to write a generic class that overload operators. 我是Scala的新手,我想编写一个重载运算符的通用类。 But I want to limit the types to Int, Long and Double. 但我想将类型限制为Int,Long和Double。 Is it possible to create an upper type bound for primitive types? 是否可以为原始类型创建上限类型?

Here is what I have done for Int. 这是我为Int做的事情。 I need to make this class generic and it should only accept Int, Long and Double. 我需要使此类通用,并且只应接受Int,Long和Double。 If this is not the right approach I hope someone can give an alternative solution for the problem. 如果这不是正确的方法,我希望有人可以为该问题提供替代解决方案。

class MyClass(value: Int) {
  def +(operand: Int): MyClass = { 
    return new MyClass(value+operand);
  }

  def -(operand: Int): MyClass = {
    return new MyClass(value-operand);
  }

  def *(operand: Int): MyClass = {
    return new MyClass(value*operand);
  }

  def /(operand: Int): MyClass = {
    return new MyClass(value/operand);
  }
} 

You could use the Type Class Pattern . 您可以使用Type Class Pattern I would require an implicit MyClassOperators[T] to be in scope, and implement it only for the desired Int , Long and Double . 我将要求隐式MyClassOperators[T]处于范围内,并且仅对所需的IntLongDouble实施它。

Define the trait implementing the operations: 定义实现操作的特征:

object MyClassImplicits {
  trait MyClassOperators[T <: AnyVal] {
    def +(firstOperand: T, secondOperand: T): MyClass[T]

    def -(firstOperand: T, secondOperand: T): MyClass[T]

    def *(firstOperand: T, secondOperand: T): MyClass[T]

    def /(firstOperand: T, secondOperand: T): MyClass[T]
  }

Now lets provide a concrete implementation for Int : 现在让我们为Int提供一个具体的实现:

implicit object MyClassIntOperators extends MyClassOperators[Int] {
  override def +(firstOperand: Int, secondOperand: Int): MyClass[Int] = new MyClass[Int](firstOperand + secondOperand)

  override def -(firstOperand: Int, secondOperand: Int): MyClass[Int] = new MyClass[Int](firstOperand - secondOperand)

  override def *(firstOperand: Int, secondOperand: Int): MyClass[Int] = new MyClass[Int](firstOperand * secondOperand)

  override def /(firstOperand: Int, secondOperand: Int): MyClass[Int] = new MyClass[Int](firstOperand / secondOperand)
}

And change the definition of MyClass to require the implicit MyClassOperators[T] to be in scope: 并更改MyClass的定义以要求隐式MyClassOperators[T]在范围内:

class MyClass[T <: AnyVal : MyClassOperators](val value: T) {
    def doSomeAddition(otherValue: T): MyClass[T] = {
      implicitly[MyClassOperators[T]].+(value, otherValue)
    }
}

When we run this with an Int instance: 当我们使用一个Int实例运行它时:

def main(args: Array[String]): Unit = {
  val myClassOfInt = new MyClass[Int](1)
  println(myClassOfInt.doSomeAddition(2).value)
}

Yields: 产量:

3

But when we try this with a Double instance (which isn't implemented yet): 但是,当我们尝试使用Double实例(尚未实现)时:

[error] could not find implicit value for evidence parameter of type tests.NewTest.MyClassImplicits.MyClassOperators[Double]
[error]     val myClassOfInt = new MyClass[Double](1.0)
[error]                        ^
[error] one error found

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

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