class Test[F[_] : FlatMap : Applicative]
脱去糖衣
class Test[F[_]](implicit flatMap: FlatMap[F], applicative: Applicative[F])
如果你对理解和语法进行分析,你会发现问题:
def testFlatApplicative: F[Unit] =
FlatMap[F].flatMap(t1)(_ =>
Functor[F].map(t2)(_ => ())
)
Error: ambiguous implicit values:
both value applicative in class Test of type cats.Applicative[F]
and value flatMap in class Test of type cats.FlatMap[F]
match expected type cats.Functor[F]
因此,您可以手动解决歧义:
def testFlatApplicative: F[Unit] =
FlatMap[F].flatMap(t1)(_ =>
applicative.map(t2)(_ => ())
)
或
def testFlatApplicative: F[Unit] =
FlatMap[F].flatMap(t1)(_ =>
flatMap.map(t2)(_ => ())
)
当你写作时,没有这样的问题
class Test[F[_]: Monad]
既然你有单隐。