Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird nested structural type in generics

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]]
like image 633
Robert Zaremba Avatar asked Oct 20 '25 21:10

Robert Zaremba


1 Answers

The signature of the Functor type constructor shows that it is parameterised with another, unary, type constructor F:

trait Functor[F[_]] extends InvariantFunctor[F]

Neither R => A nor Function1[R,A] are type constructors; they take no parameters.

However in:

type λ[α] = (R) => α

λ is a type constructor taking one parameter, α. (R is 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.

like image 150
Ben James Avatar answered Oct 24 '25 02:10

Ben James



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!