【发布时间】:2015-04-08 20:21:56
【问题描述】:
我有以下:
class FooList[T] (data: List[T]) {
def foo (f: (T, T) => T, n: Int) = data.reduce(f)
def foo (f: (T, T) => T, s: String) = data.reduce(f)
}
class BarList[T] (data: List[T]) {
def bar(f: (T, T) => T, n: Int) = data.reduce(f)
}
BarList 工作正常,FooList 失败:
scala> new FooList(List(1, 2, 3)).foo(_+_, 3)
<console>:9: error: missing parameter type for expanded function ((x$1, x$2) => x$1.$plus(x$2))
new FooList(List(1, 2, 3)).foo(_+_, 3)
scala> new FooList(List(1, 2, 3)).foo(_+_, "3")
<console>:9: error: missing parameter type for expanded function ((x$1, x$2) => x$1.$plus(x$2))
new FooList(List(1, 2, 3)).foo(_+_, "3")
^
scala> new BarList(List(1, 2, 3)).bar(_+_, 3)
res2: Int = 6
为什么FooList.foo 不起作用?
有什么方法可以让两个 FooList 调用同时工作?
【问题讨论】:
-
有点奇怪,因为
T已经在构造函数中确定了。也许重载会混淆类型推断器。
标签: scala generics type-inference overloading