开发者

just another canBuildFrom question

开发者 https://www.devze.com 2023-02-08 00:04 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消