相关疑难解决方法(0)

通常采用类型并返回相同类型的函数

我很难理解为什么Scala编译器对这个函数定义不满意:

def trimNonWordCharacters[T <: Iterable[String]](items: T): T =
     items map { _.replaceAll("\\W", "") }
Run Code Online (Sandbox Code Playgroud)

这是REPL输出:

scala> def trimNonWordCharacters[T <: Iterable[String]](items: T): T =
     items map { _.replaceAll("\\W", "") }
<console>:5: error: type mismatch;
 found   : Iterable[java.lang.String]
 required: T
       def trimNonWordCharacters[T <: Iterable[String]](items: T): T = items map { _.replaceAll("\\W", "") }
Run Code Online (Sandbox Code Playgroud)

目标是传递Iterable的任何实现并获得相同类型的退出.这可能吗?

generics collections type-theory functional-programming scala

6
推荐指数
1
解决办法
1158
查看次数