Scala: question marks in type parameters

╄→гoц情女王★ 提交于 2019-12-10 12:45:22

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/38619516/scala-question-marks-in-type-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!