首页 > 解决方案 > 如何通过scala中的类方法传递类型参数?

问题描述

在我的项目中,我需要编写一个泛型类,它在单个方法中通过其处理程序以特殊方式处理某些类型(在示例中使用数字是为了清楚起见)。

class A[T](val a:T){
    def doSomething(b:T):T = a match{
        case a : Int    => doSomethingWithIntOrDouble(b)
        case a : Double => doSomethingWithIntOrDouble(b)
        case _          => b
    }
    def doSomethingWithIntOrDouble(b:T)(implicit ev:Numeric[T]):T = 
        ev.plus(a,b)
}

<console>:13: error: could not find implicit value for parameter ev: Numeric[T]
                case a : Int    => doSomethingWithIntOrDouble(b)
                                                             ^
<console>:14: error: could not find implicit value for parameter ev: Numeric[T]
                case a : Double => doSomethingWithIntOrDouble(b)


我认为发生这种情况是因为编译器选择了类型参数而不是实际参数。告诉我,有没有办法解决这个问题?

PS好吧如果我们假设答案是正确的,那么就需要重载dsomething方法来实现多态性。

class A[T](val a:T){
    def doSomething(b:T)(implicit ev:Numeric[T]):T = ev.plus(a,b)
    def doSomething(b:T):T = b
}

但在这种情况下,又出现了另一个问题。

scala> a.doSomething(2)
<console>:13: error: ambiguous reference to overloaded definition,
both method doSomething in class A of type (b: Int)Int
and  method doSomething in class A of type (b: Int)(implicit ev: Numeric[Int])Int
match argument types (Int)
       a.doSomething(2)

标签: scalagenericsimplicit

解决方案


我不完全确定这是你想要的,但我希望它有所帮助。

基本上,您需要将T类型是Numeric的证据转发给外部方法。但是,您还必须处理不是的情况。
对于这种情况,您可以为隐式参数提供默认值,如下所示:

class A[T](val a: T) {
  def doSomething(b: T)(implicit ev: Numeric[T] = null): T = Option(ev) match {
    case Some(ev) => doSomethingWithNumeric(b)(ev)
    case None     => b
  }

  def doSomethingWithNumeric(b: T)(implicit ev: Numeric[T]): T = 
    ev.plus(a, b)
}

它似乎工作。

(new A(10)).doSomething(100) // res: Int = 110
(new A("hey")).doSomething("world") // res: String = "world"

请注意,如果您将有许多方法,那么最简洁的解决方案可能是使用两种实现来制作A特征一种用于数字类型,另一种用于无数字类型。
将两个子类的构造函数设为私有,并在请求隐式数字参数的伴生对象中创建工厂A如果找到,它将返回数字子类的新实例。


推荐阅读