Search code examples
scalascalaztype-parameter

Scala: question marks in type parameters


I'm trying to understand the following piece of code (from the Scalaz library):

def kleisliIdApplicative[R]: Applicative[Kleisli[Id, R, ?]] = ...

I'm assuming that a type of the form T[P0, ?] is a type-constructor that takes a parameter. However I'm no able to find documentation that explains the usage of question marks in type parameters.

A related question is what is the difference between the question mark and an underscore?

Is there a place where all this is well-documented?


Solution

  • The question mark syntax comes from a compiler plugin called kind-projector.

    You can see it being included in the scalaz build here: https://github.com/scalaz/scalaz/blob/series/7.3.x/project/build.scala#L310

    The plugin translates

    Kleisli[Id, R, ?]
    

    into (roughly)

    ({type L[A] = Kleisli[Id, R, A]})#L
    

    which is a rather convoluted way (but unfortunately the only way in Scala) of expressing a type lambda, i.e. a partially applied type constructor.