Created
June 20, 2010 14:21
-
-
Save retronym/445874 to your computer and use it in GitHub Desktop.
So close, yet so far, to a worthy pimp for the Traversables...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package scalaz | |
import collection.generic.CanBuildFrom | |
class TraversableW[Coll, A](val value: Coll)(implicit ev: Coll <:< Traversable[A]) { | |
def as[CC[X] <: Traversable[X]](implicit cbf1: CanBuildFrom[Coll, A, CC[A]] = null, cbfDefault: CanBuildFrom[Nothing, A, CC[A]]): CC[A] = { | |
Option(cbf1) match { | |
case Some(cbf) => | |
// A direct builder exists from the source collection to the target | |
cbf(value).result | |
case _ => | |
// No direct builder, we have to add elements one at a time. | |
val builder = cbfDefault.apply | |
value.foreach(a => builder += a) | |
builder.result | |
} | |
// def to[Result](implicit cbf: CanBuildFrom[Nothing, A, Result]): Result = value.map(identity)(collection.breakOut) | |
} | |
} | |
trait TraversableWs { | |
implicit def TraversableWTo[Coll, A](t: Coll)(implicit ev: Coll <:< Traversable[A]): TraversableW[Coll, A] = new TraversableW[Coll, A](t) | |
} | |
object Test extends Application { | |
val i = new TraversableWs {} | |
import i._ | |
TraversableWTo(List(1)).as[Vector] | |
TraversableWTo(Map(1 -> 2)).as[Vector] | |
// error: could not find implicit value for parameter ev: <:<[List[Int],Traversable[A]] List(1).as[Vector] | |
List(1).as[Vector] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment