Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the ? type?

I am trying to implement a cats Monad instance for a type that has multiple type parameters. I looked at the cats Either instance to see how it was done there. Part of the Either Monad instance code from cats is copied below:

import cats.Monad

object EitherMonad {
  implicit def instance[A]: Monad[Either[A, ?]] =
    new Monad[Either[A, ?]] {
      def pure[B](b: B): Either[A, B] = Right(b)

      def flatMap[B, C](fa: Either[A, B])(f: B => Either[A, C]): Either[A, C] =
        fa.right.flatMap(f)
    }
}

It fails to compile with the error: error: not found: type ?

What is the ? type and how can I use it when creating instances for my own types?

like image 473
mushroom Avatar asked Dec 20 '15 21:12

mushroom


People also ask

What is mean by a type?

Noun. type, kind, sort, nature, description, character mean a number of individuals thought of as a group because of a common quality or qualities.

What is one example of a type?

The definition of a type means people, places or things that share traits which allow them to belong to the same group. An example of type is men with blond hair.

What is the type of word?

Types of words can be described as the eight parts of speech in the English language: noun, pronoun, verb, adjective, adverb, preposition, conjunction, and interjection.

What is type and kind?

Sort, type and kind all generally mean the same thing. They are words we use to refer to a group of people or things which share the same characteristics. We use these words very often when we describe things and we often find them in dictionary definitions: Jazz isn't the sort of music I can listen to for very long.


1 Answers

It is special syntax for so-called type lambdas that is added by the kind projector plugin.

Either[A, ?]

is a shortcut for

({type L[X] = Either[A, X]})#L

The whole code desugars to

import cats.Monad

object EitherMonad {
  implicit def instance[A]: Monad[({type L[X] = Either[A, X]})#L] = new Monad[({type L[X] = Either[A, X]})#L] {
    def pure[B](b: B): Either[A, B] = Right(b)

    def flatMap[B, C](fa: Either[A, B])(f: B => Either[A, C]): Either[A, C] =
      fa.right.flatMap(f)
  }
}

Type lambdas look frightening, but they are essentially a very simple concept. You have a thing that takes two type parameters, like Either[A, B]. You want to provide a monad instance for Either, but trait Monad[F[_]] takes only one type parameter. But in principle that's OK, since your monad instance is only concerned with the second (the "right") type argument anyway. A type lambda is just a way to "fix" the first type argument so you have the right shape.

If you would do the same thing at value level, you wouldn't even think about it twice. You got a function of two arguments

val f: (Int, Int) => Int = ...

And something you want to pass f to, which only takes 1 argument

def foo(x: Int => Int) = ...

The only way to make things fit is to fix one of the arguments

foo(x => f(1, x))

And that's exactly what a type lambda does at type level.

like image 111
Rüdiger Klaehn Avatar answered Oct 10 '22 19:10

Rüdiger Klaehn