Estoy teniendo un tiempo difícil entender por qué el compilador Scala no está contenta con esta función definición:de funciones, que genéricamente toma un tipo y devuelve el mismo tipo
def trimNonWordCharacters[T <: Iterable[String]](items: T): T =
items map { _.replaceAll("\\W", "") }
Aquí es el REPL de salida:
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", "") }
El objetivo es pasar cualquier implementación de un Iterable y obtener el mismo tipo de retroceso. es posible?
duplicados de http://stackoverflow.com/questions/8235462/returning-original-collection-type-in-generic -method –
@LuigiPlinge Esa pregunta no necesitaba 'CanBuildFrom', ya que' filter' no lo requiere. Esta pregunta es muy similar, y el título de esa pregunta ciertamente la cubre, pero aquí se requiere un poco más para que funcione. –