Difference between [A: C] and [A[_]: C] context bounds

眉间皱痕 提交于 2021-01-29 22:00:38

问题


I'm a newbie, according to my lectures : class Test [T: Comparing] means that it requires an implicit value of type Comparing[T] that can be used in the methods of that class. With this Higher kinded type notation

Question : What does this expression def notation[F[_]: Sync] : F[Unit] = ??? refer to ?


回答1:


Consider the difference between concrete type and type constructor

Int         // concrete type
List[Int]   // concrete type
List        // type constructor

We represent the shape of the type constructor using notation 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

We could read type parameter clause [F[_]: Bar] as meaning

  • method requires implicit value of type Bar[F] where F is a type constructor
  • type constructor F has to be a member of the typeclass 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

Applying the above concepts to def notation[F[_]: Sync] we see that type constructor F has to be a member of typeclass Sync in order to call notation.



来源:https://stackoverflow.com/questions/62358022/difference-between-a-c-and-a-c-context-bounds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!