Redefining the string index operator

You may override String.get and String.set.

module Hashtbl = struct
  include Hashtbl
  module Infix = struct
    module String = struct
      include String
      let get = Hashtbl.find
      let set = Hashtbl.replace
    end
  end
end

let x = Hashtbl.create 10

open Hashtbl.Infix

let () =
  x.["test"] <- "hello";
  print_endline x.["test"]

(EDIT: it was already discussed here, Syntaxic sugar: String.set -> Bytes.set? - #17 by thierry-martinez)

3 Likes