Accepted answer

This is a special case of structural typing but called a 'type lambda', if you search for type lambda scala google will give you some results.

In short, it's used in a similar fashion as partially applied functions.

def x(a:Int, b:Int):Int = a * b
val x10 = x(10, _:Int)
x10(2) // 2

An example with types.

type IntEither[B] = Either[Int, B]
val y:IntEither[String] // Either[Int, String]

In some cases methods or classes expect a type with a single parameter

class Test[F[_]]

You can not give the Test class an Either because Test expects a type with 1 parameter and Either has 2. In order to be able to pass in an Either we can partially apply it

type X[B] = Either[Int, B]
new Test[X]

An alternative way of writing it is like this:

type X = {
  type T[x] = Either[Int, x]

new Test[X#T]

Instead of creating a type alias, we can also define the X type anonymously

new Test[({type T[x] = Either[Int, x]})#T]

These all give you an instance of type Test[Either[Int, x]].


Your example could look like this:

type PartiallyTypedFunction[R] = {
  type T[x] = R => x 

implicit def FunctionFunctor[R] =
  new Functor[PartiallyTypedFunction[R]#T] {
    def map[A, B](fa: R => A)(f: A => B): R => B = (x => f(fa(x)))

Related Query

More Query from same tag