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, *]]
Defining a generic classGeneric classes take a type as a parameter within square brackets [] . One convention is to use the letter A as type parameter identifier, though any parameter name may be used. This implementation of a Stack class takes any type A as a parameter.
Most Scala generic classes are collections, such as the immutable List, Queue, Set, Map, or their mutable equivalents, and Stack. Collections are containers of zero or more objects. We also have generic containers that aren't so obvious at first.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With