【问题标题】:Scala type alias naming rules?Scala 类型别名命名规则?
【发布时间】:2014-04-27 23:52:42
【问题描述】:

我尝试进行 Google 搜索,但由于涉及符号系统,我未能找到任何相关内容。

我知道这会编译type ==>[A, B] = Map[A, B],但这不会编译type m==>[A, B] = Map[A, B]。此外,这不会编译type =m=>[A, B] = Map[A, B]

另外,我知道这些是等效的 def foo: Int ==> String = ???def foo: ==>[Int, String] = ???。但是我找不到任何指定这些规则的官方文档?在哪里指定我可以将 2 个类型参数替换为类型别名的任一侧?如果我有 3 个类型参数怎么办?如果我有这个:type ==>[A, B, C],我该怎么做类似def foo: A ==> B ==> C

【问题讨论】:

  • 除了 Erik 的回答:关于标识符合法语法的文档是 Scala 语言规范的Chapter 1.1
  • 关于中缀类型的文档是SLS的Chapter 3.2.8

标签: scala naming type-alias


【解决方案1】:

Scala 标识符名称不能包含符号和字符的混合。这同样适用于方法以及类型和类型构造函数名称。

scala> type =m=>[A, B] = Map[A, B]
<console>:1: error: identifier expected but '=' found.
       type =m=>[A, B] = Map[A, B]
            ^

scala> type ===>[A, B] = Map[A, B]
defined type alias $eq$eq$eq$greater

scala> type mmmm[A, B] = Map[A, B]
defined type alias mmmm

比较:

scala> def foo = 3
foo: Int

scala> def === = 3
$eq$eq$eq: Int

scala> def =foo= = 3
<console>:1: error: identifier expected but '=' found.
       def =foo= = 3
           ^

唯一的例外是foo_&lt;symbol-here&gt;,但这无助于您命名=m=&gt;

scala> def foo_? = 3
foo_$qmark: Int

scala> def foo_* = 3
foo_$times: Int

scala> def foo_==> = 3
foo_$eq$eq$greater: Int

【讨论】:

  • 感谢您的回答。我的问题的第二部分呢? A ==&gt; B 的规则在哪里与 ==&gt; [A, B] 定义的相同?如果我有type ==&gt;[A, B, C] 怎么办?如何将类型别名放在类型参数“之间”?
  • 这应该可以帮助您入门:stackoverflow.com/questions/3200380/…
  • 啊,这些被称为“中缀类型语法”
猜你喜欢
  • 2019-01-29
  • 2016-01-04
  • 2013-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-08
  • 2018-04-21
  • 2012-11-09
相关资源
最近更新 更多