Can someone explain weird construction of structural type nested in generics:
implicit def Function1Functor[R]: Functor[({type λ[α]=(R) => α})#λ] =
new Functor[({type λ[α]=(R) => α})#λ] ....
This example comes from Scalaz library: Functor.scala
Why this construction is needed there? Wouldn’t be simpler to write:
implicit def Function1Functor[R,A]: Functor[R =>A]
or
implicit def Function1Functor[R,A]: Functor[Function1[R,A]]
The signature of the
Functortype constructor shows that it is parameterised with another, unary, type constructorF:Neither
R => AnorFunction1[R,A]are type constructors; they take no parameters.However in:
λis a type constructor taking one parameter,α. (Ris already defined in this context.)The syntax
({type λ[α]=(R) => α})#λis known as a type lambda. It is a syntactic trick allowing a type alias to be created inline and referred to via a projection, so the whole expression can be used where a type is required.