In Scala, is there a shorthand for reducing a generic type's arity?

我是研究僧i 提交于 2019-12-04 07:55:31

问题


I want to call Scalaz's pure method to put a value into the State monad. The following works:

type IntState[A] = State[Int, A]
val a = "a".pure[IntState]
a(1)
    (Int, java.lang.String) = (1,a)

I can also eliminate the type alias (thanks Scalaz's Pure.scala):

val a = "a".pure[({type T[A]=State[Int,A]})#T]
a(1)
    (Int, java.lang.String) = (1,a)

But that is extremely clunky. Is there a shorter way to synthesize a type like this? Like placeholder syntax for function literals, is there something like:

"a".pure[State[Int, *]]

回答1:


For concise partial type application (arity-2) in Scala, you can infix type notation as followings.

type ![F[_, _], X] = TF { type ![Y] = F[X,  Y] }

"a".pure[(State!Int)# !]

Note that we can infix notation for two arity type constructor (or type alias).




回答2:


Not sure if this qualifies as better, but here is one approach that @kmizu tweeted the other day:

scala> trait TF {
     |   type Apply[A]
     | }
defined trait TF

scala> type Curried2[F[_, _]] = TF {
     |   type Apply[X] = TF {
     |     type Apply[Y] = F[X, Y]
     |   }
     | }
defined type alias Curried2

scala> "a".pure[Curried2[State]#Apply[Int]#Apply]
res7: scalaz.State[Int,java.lang.String] = scalaz.States$$anon$1@1dc1d18

You can make it look a little nicer by using symbolic type aliases.

scala> type ![F[_, _]] = TF {
     |   type ![X] = TF {
     |     type ![Y] = F[X, Y]
     |   }
     | }
defined type alias $bang

scala> "a".pure[![State]# ![Int]# !]
res9: scalaz.State[Int,java.lang.String] = scalaz.States$$anon$1@1740235



回答3:


the most popular way of reducing arity is kind-projector (https://github.com/non/kind-projector) plugin that is also used in cats library. By enabling this plugin your example can be transformed to:

val a = "a".pure[State[Int, ?]]

Note: this syntax will be enabled in Dotty by default.



来源:https://stackoverflow.com/questions/7643493/in-scala-is-there-a-shorthand-for-reducing-a-generic-types-arity

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