我很难理解为什么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