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

前端 未结 3 888
灰色年华
灰色年华 2021-01-31 22:29

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[I         


        
相关标签:
3条回答
  • 2021-01-31 22:54

    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).

    0 讨论(0)
  • 2021-01-31 23:02

    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
    
    0 讨论(0)
  • 2021-01-31 23:15

    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.

    0 讨论(0)
提交回复
热议问题