Daniel Sobral showed how we can create a method that returns the same collection type upon which it was called in his answer to this question: Returning original collection type in generic method
Is it possible to do the same for a method that uses a mapping to return a differently-parameterized version of the same collection type?
For example
def foo[A](xs: GenTraversable[A]) = xs map (_.toString)
foo( List(1, 2, 3) )
returns res: GenTraversable[String] = List(1, 2, 3)
Can this be adapted so that
foo( Set(1, 2, 3) )
returns a Set[String]
foo( List(1, 2, 3) )
returns a List[String]
foo( Vector(1, 2, 3) )
returns a Vector[String]
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…