just another canBuildFrom question
I'm going throught collection api and the returned type of operations which can modify the type of conta开发者_开发知识库iner and the type of contained element have all the header in its non-usecase form similar to the following:
def map [B, That] (f: (A) ⇒ B)(implicit bf: CanBuildFrom[List[A], B, That]):That
It's clear that there is sure 'fallback' to other collection type with That
(as in case of BitSet
fallbacking to Set
if the contained item is changed to other than Int
). But how can one be sure of the type of the item? I would expect the return type to That[B]
, but it is not. Why? Educate me please :-)You already gave the answer yourself: If you want to be able to return a BitSet
in appropriate cases, you need to get rid of that type parameter in the return type. There is no BitSet[Int]
, obviously.
All the information you need is already handled by the appropriate CanBuildFrom
implicits which have a fallback definition of the type CanBuildFrom[Coll, A, Traversable[A]]
(or whatever may be over Traversable
in the type hierarchy).
Of course, the CanBuildFrom
must be sensible but I think it’s actually possible to abuse it and return a truly strange That
. Nonetheless, the defaults are pretty sane.
精彩评论