diff options
| author | Florian Angeletti <octa@polychoron.fr> | 2017-09-15 18:47:05 +0200 |
|---|---|---|
| committer | Damien Doligez <damien.doligez@gmail.com> | 2017-09-15 18:47:05 +0200 |
| commit | 201ee17fe007add8f2cd4a0c0e1db133864f9b63 (patch) | |
| tree | ad16a6d6ddc3538cfb8b2256cb39ac23c1967b3f /manual | |
| parent | 20668002bba187445c7711a936dd36f7187ae612 (diff) | |
| download | ocaml-201ee17fe007add8f2cd4a0c0e1db133864f9b63.tar.gz | |
Extended indexing operators (#1064)
* Extended indexing operators
This commit adds extended indexing operators to the parser.
For instance,
```
let (.%()) p (x,y) = p.( x ).( y )
;; p.%(0,0)
```
Extended indexing operators name starts with a leading
dot "." followed by an operator symbol, except "." or "<"
and a sequence of any operators and must be closes with a
couple of enclosing parentheses ( i.e. "()", "[]", "{}" )
and then an optional assignment operator "<-":
* '.' dotsymbolchar symbolchar* '(' ')' ['<-']
* '.' dotsymbolchar symbolchar* '[' ']' ['<-']
* '.' dotsymbolchar symbolchar* '{' '}' ['<-']
Similarly, expressions of the form
* expr_1 '.' dotsymbolchar symbolchar* '(' expr_2 ')'
* expr_1 '.' dotsymbolchar symbolchar* '[' expr_2 ']'
* expr_1 '.' dotsymbolchar symbolchar* '{' expr_2 '}'
and
* expr_1 '.' dotsymbolchar symbolchar* '(' expr_2 ')' '<-' expr_3
* expr_1 '.' dotsymbolchar symbolchar* '[' expr_2 ']' '<-' expr_3
* expr_1 '.' dotsymbolchar symbolchar* '{' expr_2 '}' '<-' expr_3
are desugared to
* ('.' dotsymbolchar symbolchar* '(' ')' ) expr_1 expr_2
* ('.' dotsymbolchar symbolchar* '[' ']' ) expr_1 expr_2
* ('.' dotsymbolchar symbolchar* '{' '}' ) expr_1 expr_2
and
* ('.' dotsymbolchar symbolchar* '(' ')' '<-' ) expr_1 expr_2 expr_3
* ('.' dotsymbolchar symbolchar* '[' ']' ) expr_1 expr_2 expr_3
* ('.' dotsymbolchar symbolchar* '{' '}' '<-' ) expr_1 expr_2 expr_3
Diffstat (limited to 'manual')
| -rw-r--r-- | manual/manual/refman/expr.etex | 5 | ||||
| -rw-r--r-- | manual/manual/refman/exten.etex | 42 | ||||
| -rw-r--r-- | manual/manual/refman/lex.etex | 5 | ||||
| -rw-r--r-- | manual/manual/refman/names.etex | 3 |
4 files changed, 51 insertions, 4 deletions
diff --git a/manual/manual/refman/expr.etex b/manual/manual/refman/expr.etex index fdc3659b94..2824ae5cc0 100644 --- a/manual/manual/refman/expr.etex +++ b/manual/manual/refman/expr.etex @@ -118,8 +118,9 @@ See also the following language extensions: \hyperref[s:explicit-overriding-open]{overriding in open statements}, \hyperref[s:bigarray-access]{syntax for Bigarray access}, \hyperref[s:attributes]{attributes}, -\hyperref[s:extension-nodes]{extension nodes} and -\hyperref[s:local-exceptions]{local exceptions}. +\hyperref[s:extension-nodes]{extension nodes}, +\hyperref[s:local-exceptions]{local exceptions} +\hyperref[s:index-operators]{extended indexing operators}. The table below shows the relative precedences and associativity of operators and non-closed constructions. The constructions with higher diff --git a/manual/manual/refman/exten.etex b/manual/manual/refman/exten.etex index 1f3b485b6b..a7cbe867b8 100644 --- a/manual/manual/refman/exten.etex +++ b/manual/manual/refman/exten.etex @@ -2351,3 +2351,45 @@ will be converted directly to type t = T of string [@@ocaml.doc " Attaches to t "] \end{verbatim} + +\section{Extendend indexing operators \label{s:index-operators} } +(Introduced in 4.06) + +\begin{syntax} + +dot-ext: + | ('!'||'$'||'%'||'&'||'*'||'+'||'-'||'/'||':'||'='||'>'||'?'||'@'||'^'||'|'||'~') { operator-char } +; +expr: + ... + | expr '.' dot-ext ( '(' expr ')' || '[' expr ']' || '{' expr '}' ) [ '<-' expr ] +; +operator-name: + ... + | '.' dot-ext ('()' || '[]' || '{}') ['<-'] +; +\end{syntax} + + +This extension provides syntactic sugar for getting and setting elements +for user-defined indexed types. For instance, we can define python-like +dictionaries with +\begin{caml-example} +module Dict = struct + +include Hashtbl +let ( .%{} ) tabl index = find tabl index +let ( .%{}<- ) tabl index value = add tabl index value +end;; +let dict = + let open Dict in + let dict = create 10 in + let () = + dict.%{"one"} <- 1; + dict.%{"two"} <- 2 in + dict +;; +let () = + let open Dict in + assert( dict.%{"one"} = 1 );; +\end{caml-example} diff --git a/manual/manual/refman/lex.etex b/manual/manual/refman/lex.etex index 87634d3bdb..f1ac3ea1c9 100644 --- a/manual/manual/refman/lex.etex +++ b/manual/manual/refman/lex.etex @@ -218,8 +218,9 @@ operator-char: '/' || ':' || '<' || '=' || '>' || '?' || '@' || '^' || '|' || '~' \end{syntax} -See also the following language extension: -\hyperref[s:ext-ops]{extension operators}. +See also the following language extensions: +\hyperref[s:ext-ops]{extension operators} and +\hyperref[s:index-operators]{extended indexing operators}. Sequences of ``operator characters'', such as "<=>" or "!!", are read as a single token from the @infix-symbol@ or @prefix-symbol@ diff --git a/manual/manual/refman/names.etex b/manual/manual/refman/names.etex index bad503395b..4f85eed44a 100644 --- a/manual/manual/refman/names.etex +++ b/manual/manual/refman/names.etex @@ -73,6 +73,9 @@ inst-var-name: method-name: lowercase-ident \end{syntax} +See also the following language extension: +\hyperref[s:index-operators]{extended indexing operators}. + As shown above, prefix and infix symbols as well as some keywords can be used as value names, provided they are written between parentheses. The capitalization rules are summarized in the table below. |
