在Scala中,是否有减少泛型类型的简写? [英] In Scala, is there a shorthand for reducing a generic type's arity?

查看:109
本文介绍了在Scala中,是否有减少泛型类型的简写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想调用Scalaz的纯粹的方法来为State monad添加一个值。以下工作:

 类型IntState [A] =状态[Int,A] 
val a =a .pure [IntState]
a(1)
(Int,java.lang.String)=(1,a)

我也可以消除类型别名(谢谢Scalaz的Pure.scala):

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

但这非常笨重。是否有更短的方法来合成这样的类型?类似于函数文字的占位符语法,是不是有这样的: b $ b


解决方案

对于Scala中的简洁部分类型应用程序(arity-2) ,你可以在下面加入类型符号。

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

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

请注意,我们可以为两个arity类型的构造函数(或类型别名)插入符号。


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, *]]

解决方案

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

这篇关于在Scala中,是否有减少泛型类型的简写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆