简体   繁体   English

Erlang中的冒号和哈希运算符

[英]Colon and Hash operators in Erlang

The Erlang documentation provides a complete list of operators with precedence and associativity . Erlang文档提供了具有优先级和关联性的运算符完整列表

The first two operators in the table, : and # do not appear to be described anywhere else in the documentation. 表中的前两个运算符:#在文档的其他地方似乎都没有描述。 Technically, # looks like a binary operator in map update expressions, but that isn't quite clear. 从技术上讲, #看起来像地图更新表达式中的二进制运算符,但这还不是很清楚。 I saw no description of a : operator but might have just missed something. 我没有看到:运算符的描述,但可能只是错过了一些东西。

Does anyone have a useful technical description of the : and # operators? 有人对:#运算符有有用的技术描述吗? (I started looking in the Erlang Grammar but there are over 700 occurrences of the colon, making a search pretty difficult.) (我开始在Erlang语法中查找,但是冒号出现了700多次,因此搜索非常困难。)

For me it returns just 14 occurrences, much less than 700 :) 对我来说,它只返回14次,远少于700次:)

:~/otp/lib/stdlib/src % cat erl_parse.yrl | grep "':'"
'(' ')' ',' '->' '{' '}' '[' ']' '|' '||' '<-' ';' ':' '#' '.'
spec_fun ->                  atom ':' atom : {'$1', '$3'}.
spec_fun -> atom ':' atom '/' integer '::' : {'$1', '$3', '$5'}.
type -> atom ':' atom '(' ')'             : {remote_type, ?anno('$1'),
type -> atom ':' atom '(' top_types ')'   : {remote_type, ?anno('$1'),
bin_base_type -> var ':' type          : build_bin_type(['$1'], '$3').
bin_unit_type -> var ':' var '*' type  : build_bin_type(['$1', '$3'], '$5').
expr_800 -> expr_max ':' expr_max :
opt_bit_size_expr -> ':' bit_size_expr : '$2'.
bit_type -> atom ':' integer : { element(3,'$1'), element(3,'$3') }.
fun_expr -> 'fun' atom_or_var ':' atom_or_var '/' integer_or_var :
try_clause -> atom ':' expr clause_guard clause_body :
try_clause -> var ':' expr clause_guard clause_body :
inop_prec(':') -> {900,800,900};
:~/otp/lib/stdlib/src % cat erl_parse.yrl | grep "':'" | wc -l
      14

Also: 也:

:~/otp/lib/stdlib/src % cat erl_parse.yrl | grep "'#'" 
'(' ')' ',' '->' '{' '}' '[' ']' '|' '||' '<-' ';' ':' '#' '.'
type -> '#' '{' '}'                       : {type, ?anno('$1'), map, []}.
type -> '#' '{' map_pair_types '}'        : {type, ?anno('$1'), map, '$3'}.
type -> '#' atom '{' '}'                  : {type, ?anno('$1'), record, ['$2']}.
type -> '#' atom '{' field_types '}'      : {type, ?anno('$1'),
map_expr -> '#' map_tuple :
map_expr -> expr_max '#' map_tuple :
map_expr -> map_expr '#' map_tuple :
record_expr -> '#' atom '.' atom :
record_expr -> '#' atom record_tuple :
record_expr -> expr_max '#' atom '.' atom :
record_expr -> expr_max '#' atom record_tuple :
record_expr -> record_expr '#' atom '.' atom :
record_expr -> record_expr '#' atom record_tuple :
inop_prec('#') -> {800,700,800};
-type pre_op() :: 'catch' | '+' | '-' | 'bnot' | 'not' | '#'.
preop_prec('#') -> {700,800}.
-type type_preop() :: '+' | '-' | 'bnot' | '#'.
type_inop_prec('#') -> {800,700,800}.
type_preop_prec('#') -> {700,800}.

There isn't really a better place for such info than erl_parse.yrl . 此类信息的确比erl_parse.yrl That's not my opinion BTW. 顺便说一句, 这不是我的看法

Neither of them are proper operators. 他们都不是正确的操作员。 The documentation is misleading here. 该文档在这里具有误导性。 The # character is used in base-N-literals, as in 16#ffff, but that's on the lexical level, and in the syntax for records and maps, but not as an actual operator. 与16#ffff一样,#字符用于基本N字面量,但这是在词汇级别上,在记录和地图的语法中,而不是实际的运算符。 The : character is used in remote calls, as in lists:reverse(Xs), but again, it's not really an operator - "lists:reverse" isn't in itself a subexpression with a value. :字符用于远程调用中,就像在list:reverse(Xs)中一样,但是同样,它并不是真正的运算符-“ lists:reverse”本身并不是具有值的子表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM