8.20 Extended indexing operators
(Introduced in 4.06)
|
This extension provides syntactic sugar for getting and setting elementsfor user-defined indexed types. For instance, we can define python-likedictionaries with
module Dict = struct include Hashtbl let ( .%{} ) tabl index = find tabl index let ( .%{}<- ) tabl index value = add tabl index value end let dict = let dict = Dict.create 10 in let () = dict.Dict.%{"one"} <- 1; let open Dict in dict.%{"two"} <- 2 in dict
dict.Dict.%{"one"};; - : int = 1
let open Dict in dict.%{"two"};; - : int = 2