在 Scala 中,是否有减少泛型类型数量的简写?
我想调用 Scalaz 的 pure
方法将值放入 State monad 中。以下工作:
type IntState[A] = State[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]})#T]
a(1)
(Int, java.lang.String) = (1,a)
但这非常笨重。有没有更短的方法来合成这样的类型?就像函数文字的占位符语法一样,是否有类似以下内容的内容:
"a".pure[State[Int, *]]
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, *]]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于 Scala 中简洁的部分类型应用(arity-2),您可以按如下方式添加类型表示法。
请注意,我们可以为两个类型构造函数(或类型别名)添加中缀表示法。
For concise partial type application (arity-2) in Scala, you can infix type notation as followings.
Note that we can infix notation for two arity type constructor (or type alias).
不确定这是否更好,但这是@kmizu 前几天在 Twitter 上发布的一种方法:
您可以通过使用符号类型别名使其看起来更好一点。
Not sure if this qualifies as better, but here is one approach that @kmizu tweeted the other day:
You can make it look a little nicer by using symbolic type aliases.
减少数量的最流行的方法是 kind-projector (https://github.com/non/kind-投影仪)插件,也用于 cats 库。通过启用此插件,您的示例可以转换为:
注意:默认情况下,此语法将在 Dotty 中启用。
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:
Note: this syntax will be enabled in Dotty by default.