gpt4 book ai didi

scala - 为什么方法参数 F 可以与类型构造函数 F 同名?

转载 作者:行者123 更新时间:2023-12-04 23:35:30 29 4
gpt4 key购买 nike

我正在看 John De Goes “FP to the Max”视频。在代码中,他做了这样的事情来获取隐式对象:

  object Program {
def apply[F[_]](implicit F: Program[F]): Program[F] = F
}

这是否意味着变量名 F( implicit F: Program[F] 中的第一个)实际上是不同的 F ?这很令人困惑。他的意思是:
  object Program {
def apply[F[_]](implicit ev: Program[F]): Program[F] = ev
}

编译器怎么知道哪个 F他在返回 F 时指的是?类型构造函数还是作用域中的变量?

最佳答案

确实是函数参数T与类型参数 T 不同, 例如

def f[T](T: T): T = T
f(42) // res0: Int = 42

编译器不会混淆,因为值存在于 different universe 中从类型:

...there exist two separate universes, the universe of types and the universe of values. In the universe of values, we have methods which take values as arguments in round parentheses (or occasionally curly braces). In the universe of types, we have type constructors, which take types as arguments in square brackets.



在处理类型类时有时会使用此约定。它旨在传达我们只想返回解析为 F 的类型类实例。 .为避免混淆,您可以使用 ev方法你已经提出的问题,甚至
object Program {
def apply[F[_]: Program]: Program[F] = implicitly[Program[F]]
}

作为旁注,这个技巧与 apply typeclass 的伴生对象中的方法允许我们避免使用 implicitly ,例如,给定
trait Foo[T]
trait Bar[T]

trait Program[F[_]]
implicit val fooProgram: Program[Foo] = ???
implicit val barProgram: Program[Bar] = ???

object Program {
def apply[F[_]: Program]: Program[F] = implicitly
}

然后我们可以写
Program[Bar]

代替
implicitly[Program[Bar]]

关于scala - 为什么方法参数 F 可以与类型构造函数 F 同名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59162955/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com