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

前端 未结 3 922
灰色年华
灰色年华 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 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
    

提交回复
热议问题