简体   繁体   English

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

[英]Why can method parameter F have the same name as type constructor F?

I was watching John De Goes "FP to the Max" video.我正在看 John De Goes “FP to the Max”视频。 In the code he does something like this to get the implicit object:在代码中,他做了这样的事情来获取隐式对象:

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

Does this imply that the variable name F (the first one in implicit F: Program[F] ) is actually a different F ?这是否意味着变量名 F ( implicit F: Program[F]的第一个)实际上是一个不同的F It is very confusing.这很令人困惑。 Does he mean to do:他的意思是:

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

How does the compile know which F he is referring to while returning the F ?如何编译知道F他是指在返回的F The type constructor or the variable in scope?类型构造函数还是作用域中的变量?

Indeed the function parameter T is different from type parameter T , for example事实上,函数参数T不同于类型参数T ,例如

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

Compiler does not get confused because values exist in a different universe from types :编译器不会混淆,因为类型存在于不同的宇宙中

...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.在类型的世界中,我们有类型构造函数,它将类型作为方括号中的参数。

This convention is sometimes used when dealing with typeclasses.在处理类型类时有时会使用此约定。 It is meant to communicate that we want to simply return the typeclass instance resolved for F .它旨在传达我们想要简单地返回为F解析的类型类实例。 To avoid confusion you could use ev approach you already suggested in question, or even为避免混淆,您可以使用您已经建议的ev方法,甚至

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

As a side-note, this trick with apply method in companion object of typeclass allows us to avoid having to use implicitly , for example, given作为旁注,这个在 typeclass 的伴生对象中使用apply方法的技巧允许我们避免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
}

then we can write然后我们可以写

Program[Bar]

instead of代替

implicitly[Program[Bar]]

How does the compile know which F he is referring to while returning the F ?如何编译知道F他是指在返回的F The type constructor or the variable in scope?类型构造函数还是作用域中的变量?

The compiler knows that a method/function can only return a value.编译器知道一个方法/函数只能返回一个值。 So what follows the = has a type but is not a type.所以=后面的一个类型但不是一个类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 值不是类型参数 F[] 的成员 - value is not a member of type parameter F[] 为什么正确的类型 Any 和 Nothing 适合类型构造函数形状 F[_] 当它们是不同的类型时? - Why do proper types Any and Nothing fit type constructor shape F[_] when they are different kinds? 为什么结果不相同,f Scala中的插值器 - Why the result is not the same, f Interpolator in scala 如何使用类型依赖于隐式参数的方法参数? - How can I have a method parameter with type dependent on an implicit parameter? 如何包装有效的 F 而不是具体的类型构造函数? - How to wrap effectful F instead of the concrete type constructor? 通过类型成员而不是类型参数进行F-限制量化? - F-bounded quantification through type member instead of type parameter? F界多态性中子类型的Scala重写类型参数 - Scala rewriting type parameter of sub type in F-bounded polymorphism 当参数是对象类型时,如何在F#中要求多个接口? - How to require multiple interfaces in F# when a parameter is an object type? 强制对特征“ F”的方法进行某种类型“ T”的所有输入以前都是由“ F”本身产生的 - Enforce that all inputs of some type `T` to methods of a trait `F` have been previously produced by `F` itself 为什么在 F[_] 参数预期有效的地方传递 Int? - Why is passing Int where an F[_] parameter is expected valid?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM