简体   繁体   中英

Colon and Hash operators in Erlang

The Erlang documentation provides a complete list of operators with precedence and associativity .

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

For me it returns just 14 occurrences, much less than 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 . 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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