【问题标题】:Scala infix notation for generics泛型的 Scala 中缀表示法
【发布时间】:2019-02-08 21:53:53
【问题描述】:

可以在 Scala 中缀表示法中使用泛型吗?

例如:

// Example from play-json

Json.obj("name" -> "Joe") \ "name" as[String] // Generics in infix notation
                                              // error: type application is not allowed for postfix operators

理想情况下,我想实现如下 API:

Json.obj("name" -> "Joe") \ "name" as String // But I think its implossible

当然,使用带括号的标准点表示法可以正常工作:

(Json.obj("name" -> "Joe") \ "name").as[String]

我发现的唯一值得讨论的是:https://groups.google.com/forum/#!msg/scalatest-users/Ujn2d2MdXm0/yvHpk1pOlDMJ

【问题讨论】:

标签: scala dsl infix-notation


【解决方案1】:

中缀符号只能用于括号,不能用于捕获类型信息的方括号。如果你在 LHS 上给出类型提示,那么它可以被丢弃。

【讨论】:

    猜你喜欢
    • 2016-11-29
    • 1970-01-01
    • 1970-01-01
    • 2021-02-21
    • 1970-01-01
    • 2020-09-10
    • 1970-01-01
    • 2011-06-26
    相关资源
    最近更新 更多