我是个新手,根据我的讲课。class Test [T: Comparing]
意思是说,它需要一个隐含的类型为 Comparing[T]
可以用在该类的方法中。
问题:这句话是什么意思?def notation[F[_]: Sync] : F[Unit] = ???
指的是?
考虑具体类型和类型之间的区别 构造者
Int // concrete type
List[Int] // concrete type
List // type constructor
我们用符号来表示类型构造函数的形状。F[_]
trait Foo[T] // Foo takes any type
trait Bar[F[_]] // Bar takes any type constructor
new Foo[Int] {} // ok
new Foo[List[Int]] {} // ok
new Foo[List] {} // error
new Bar[Int] {} // error
new Bar[List[Int]] {} // error
new Bar[List] {} // ok
我们可以将类型参数子句改为 [F[_]: Bar]
为的是
Bar[F]
哪儿 F
是一个类型构造函数F
一定是 成员 类型类的 Bar
trait Bar[F[_]]
// make type constructor Foo a member of typeclass Bar
implicit val barOfFoo: Bar[Foo] = new Bar[Foo] { println("woohoo") }
def g[F[_]: Bar] = implicitly[Bar[F]]
g[Foo] // ok
g[Int] // error - type parameter is of incorrect shape
g[Foo[Int]] // error - type parameter is of incorrect shape
g[List] // error - type parameter is of correct shape but not a member of Bar
将上述概念应用于 def notation[F[_]: Sync]
我们看到,类型构造函数 F
必须是typeclass的成员 Sync
为了调用 notation
.