在 Scala 中,是否有减少泛型类型数量的简写?

发布于 2024-12-07 15:13:38 字数 461 浏览 0 评论 0原文

我想调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

独自唱情﹋歌 2024-12-14 15:13:38

对于 Scala 中简洁的部分类型应用(arity-2),您可以按如下方式添加类型表示法。

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

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

懒猫 2024-12-14 15:13:38

不确定这是否更好,但这是@kmizu 前几天在 Twitter 上发布的一种方法:

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

您可以通过使用符号类型别名使其看起来更好一点。

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

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
深海夜未眠 2024-12-14 15:13:38

减少数量的最流行的方法是 kind-projector (https://github.com/non/kind-投影仪)插件,也用于 cats 库。通过启用此插件,您的示例可以转换为:

val a = "a".pure[State[Int, ?]]

注意:默认情况下,此语法将在 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:

val a = "a".pure[State[Int, ?]]

Note: this syntax will be enabled in Dotty by default.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文