8.20 Extended indexing operators

(Introduced in 4.06)

dot-ext::=
(!∣ $∣ %∣ &∣ *∣ +∣ -∣ /∣ :∣ =∣ >∣ ?∣ @∣ ^∣ |∣ ~) { operator-char }
expr::=
expr . [module-path .] dot-ext ( ( expr ) ∣ [ expr ] ∣ { expr } ) [ <- expr ]
operator-name::=
. dot-ext (() ∣ [] ∣ {}) [<-]

This extension provides syntactic sugar for getting and setting elementsfor user-defined indexed types. For instance, we can define python-likedictionaries with

  1. module Dict = struct
  2. include Hashtbl
  3. let ( .%{} ) tabl index = find tabl index
  4. let ( .%{}<- ) tabl index value = add tabl index value
  5. end
  6. let dict =
  7. let dict = Dict.create 10 in
  8. let () =
  9. dict.Dict.%{"one"} <- 1;
  10. let open Dict in
  11. dict.%{"two"} <- 2 in
  12. dict
  13.  
  1. dict.Dict.%{"one"};;
  2. - : int = 1
  1. let open Dict in dict.%{"two"};;
  2. - : int = 2