Function which generically takes a type and returns the same type

后端 未结 2 1587
心在旅途
心在旅途 2020-12-29 15:41

I am having a tough time understanding why the Scala compiler is unhappy about this function definition:

def trimNonWordCharacters[T <: Iterable[String]](         


        
2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-29 16:29

    The map method on Iterable returns an Iterable, so even if T is a subclass of Iterable, it's map method will return Iterable.

    To get better typing, you'd have to write it like this:

    import scala.collection.IterableLike
    def trimNonWordCharacters[T <: Iterable[String]](items: T with IterableLike[String, T]): T =
         items map { _.replaceAll("\\W", "") }
    

    However, that won't work either, because there's no information that let a map on T to generate another T. For example, mapping a BitSet into a String cannot result in a BitSet. So we need something else: something that teaches how to build a T from a T, where the mapped elements are of type String. Like this:

    import scala.collection.IterableLike
    import scala.collection.generic.CanBuildFrom
    def trimNonWordCharacters[T <: Iterable[String]]
                             (items: T with IterableLike[String, T])
                             (implicit cbf: CanBuildFrom[T, String, T]): T =
         items map { _.replaceAll("\\W", "") }
    

提交回复
热议问题