Scala - Method type parameters

105 views Asked by At

I'm trying to understand some autogenerated code by the scala compiler but I don't know after what to search.

I have the following 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
}

After I decompiled the class file, the signature of the f1 and f2 methods look like:

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);
}

As you can see, the methods have an additional parameter. Where can I find some documentation about this method type parameter notation F[_, _] : Arrow1 ?

1

There are 1 answers

0
Andrey Tyukin On BEST ANSWER

You are looking for "context bounds".

When you look up "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, where it says (Quote slightly changed, simplified to case with single context bound):

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.

Here is a link to FAQ with more information on the topic.