views:

117

answers:

1

The signature of TraversableLike.flatMap is as follows:

def flatMap[B, Th](f : (A) => Traversable[B])(implicit bf : CanBuildFrom[Repr, B, Th]) : Th

The signature of GenericTraversableTemplate.flatten is:

def flatten[B](implicit asTraversable : (A) => Traversable[B]) : CC[B]

Why is the latter method (which seems to me to differ from flatMap only in the sense that the transformer function is implicit) not definable on TraversableLike as:

def flatten[B, Th](implicit asTraversable: (A) => Traversable[B], 
                   implicit bf : CanBuildFrom[Repr, B, Th]) : Th

Is there some reason that this must be the case?

+2  A: 

I think the answer lies in the source code:

def flatten[B](implicit asTraversable: A => /*<:<!!!*/ Traversable[B]): CC[B]

The implicit parameter to flatten should actually be A <:< Traversable[B] (ie. an assertion that the generic parameter of GenericTraversableTemplate is itself Traversable). (See this mailing list thread for discussion about why it's currently commented out.) My understanding is that all the methods in this trait used to be defined on (some of) the companion objects of collection classes because they were only applicable for some instantiations (if that's the right word) of type parameters. This <:< construct allows them to be made instance methods.

Ben Lings