我正在用更高级的类型浸泡我的脚趾,探索一个非常基本的Scala示例:
trait Mappable[F[_]] { def map[A, B](fa: F[A])(f: A => B): F[B] } object Mappable { implicit object MappableOption extends Mappable[Option] { def map[A, B](fa: Option[A])(f: A => B): Option[B] = fa.map(f) } implicit object MappableSeq extends Mappable[Seq] { def map[A, B](fa: Seq[A])(f: A => B): Seq[B] = fa.map(f) } } def bananaTuple[F[_], T](f: F[T])(implicit F: Mappable[F]): F[(String, T)] = F.map(f)(("banana", _))
这有效:
bananaTuple(Option(42)) // Some((banana,42)) bananaTuple(Seq(42)) // List((banana,42))
但这不编译:
bananaTuple(Some(42)) bananaTuple(List(42))
我得到的编译错误:
could not find implicit value for parameter F: ch.netzwerg.hkt.HigherKindedTypes.Mappable[Some] bananaTuple(Some(42)) not enough arguments for method bananaTuple: (implicit F: ch.netzwerg.hkt.HigherKindedTypes.Mappable[Some])Some[(String, Int)]. Unspecified value parameter F. bananaTuple(Some(42))
如何在游戏中带来变化?