Scala - 方法类型参数

Scala - Method type parameters

我试图理解一些由 scala 编译器自动生成的代码,但我不知道要搜索什么。

我有以下 class:

trait Arrow1[F[_, _]]

abstract class Test {

  def f1[F[_, _] : Arrow1, A, B, C](fa: F[A,B], fb: F[A, C]): F[A, (B, C)]

  def f2[A: Seq, B](a: A): Boolean
}

我反编译class文件后,f1和f2方法的签名如下:

public abstract class Test {
    public abstract <F, A, B, C> F f1(F var1, F var2, Arrow1<F> var3);

    public abstract <A, B> boolean f2(A var1, Seq<A> var2);
}

如您所见,这些方法有一个额外的参数。在哪里可以找到有关此方法类型参数表示法的一些文档 F[_, _] : Arrow1 ?

您正在寻找 "context bounds".

当你查找 "Type parameters" in the Spec, you encounter A: B in the first paragraph, and you also get the link to Context Bounds and View Bounds 时,它说 (引用略有改动,简化为具有单个上下文绑定的大小写)

A type parameter A of a method or non-trait class may also have one or more context bounds A : T. In this case the type parameter may be instantiated to any type S for which evidence exists at the instantiation point that S satisfies the bound T. Such evidence consists of an implicit value with type T[S].

A method or class containing type parameters with view or context bounds is treated as being equivalent to a method with implicit parameters. Consider first the case of a single parameter with [...] context bounds such as:

def f[A: U1](params): R = ...

Then the method definition above is expanded to

def f[A](params)(implicit v1: U1[A]): R  = ...

where the v1 is a fresh name for the newly introduced implicit parameter. This parameter is called evidence parameter.

这里是 link to FAQ,其中包含有关该主题的更多信息。