scala - 如何通过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)
解决方案
我不完全确定这是你想要的,但我希望它有所帮助。
基本上,您需要将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
如果找到,它将返回数字子类的新实例。
推荐阅读
- android - 为 iOS 和 Android 自动安装应用程序?
- c++ - 在 CMake 中获取包含库的包含路径列表
- javascript - 在 c# asp.net 中显示基于数据表记录的动态 HTML tr
- javascript - CSRF 验证失败。使用 Python Django 后端和 React,Axios(使用 POST)前端
- java - 相互贯穿的物体
- c++ - 在 C++17 中,这段代码是否应该产生警告?
- ios - 出口合规 iOS
- swift - 显示键盘时,文本字段会从视图中消失
- multidimensional-array - 使用 4 维数组
- javascript - 如何将图标添加到nativebase组件的日期选择器反应原生