Is it possible to “curry” higher-kinded types in Scala?

谁都会走 提交于 2019-11-30 11:58:41

问题


Let's suppose I have a trait with two type parameters, e.g.

trait Qux[A, B]

and another trait with a higher-kinded type parameter, e.g.

trait Turkle[C[_]]

I'd like to be able to substitute a fixed value for one of the type parameters for Qux, so that it can be used to parametrize Turkle.

Here's an example (of code that doesn't make sense in Scala!):

trait Baz[A] extends Turkle[Qux[A, _]]

Anyone have any ideas how to achieve this effect?


回答1:


Jason Zaugg came up with the most succinct way to do this:

trait Baz[A] extends Turkle[({type x[a]=Qux[A, a]})#x]

IntelliJ's Scala plugin will optionally collapse this to:

trait Baz[A] extends Turkle[x[a]=Qux[A, a]]



回答2:


You mean something like this?

trait QuxWithString[A] extends Qux[A, String]
new Turkle[QuxWithString]{}

This is the analog to partial application for types.




回答3:


trait Turkle[C[_]]
trait Qux[A,B]
trait Wraps[A] {
  type Jkz[X] = Qux[A,X]
  trait Baz extends Turkle[Jkz]
}



回答4:


The compiler plugin kind projector allows this as well:

// Explicit lambda, greek letters
trait Baz[A] extends Turkle[λ[α=>Qux[A,α]]]

// Explicit lambda, normal letters
trait Baz[A] extends Turkle[Lambda[a=>Qux[A,a]]]

// No explicit lambda, ? placeholder    
trait Baz[A] extends Turkle[Qux[A,?]]


来源:https://stackoverflow.com/questions/6247817/is-it-possible-to-curry-higher-kinded-types-in-scala

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