本文介绍了[A:C]和[A [_]:C]上下文界限之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的演讲,我是新手: class Test [T:Comparing] 意味着它需要一个 Comparing [T] 类型的隐式值,该值可以在该类的方法中使用.使用这种更高类型的符号

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

问题:此表达式 def符号[F [_]:Sync]:F [Unit] = ??? 指的是什么?

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

推荐答案

考虑具体类型和类型构造函数

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] 理解为

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

  • 方法需要类型为 Bar [F] 的隐式值,其中 F 是类型构造函数
  • 类型构造函数 F 必须是类型类 Bar
  • 的成员
  • 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

将以上概念应用于 def符号[F [_]:Sync] ,我们看到类型构造函数 F 必须是typeclass Sync 以调用符号.

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.

这篇关于[A:C]和[A [_]:C]上下文界限之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 16:28