Commits

Anonymous committed de9f73a

maps: +get_def

  • Participants
  • Parent commits 85fab96

Comments (0)

Files changed (1)

         method tikey : ti 'k;
         method tival : ti 'v;
 
+        method get_def : 'k -> 'v -> 'v;
         method get_all : 'k -> list 'v;
 
         inherit map_foldable ['k, 'v];
         method is_empty : bool;
         method get_exn : 'k -> 'v;
         method get_opt : 'k -> option 'v;
+        method get_def : 'k -> 'v -> 'v;
         method mem : 'k -> bool;
         method remove : 'k -> unit;
         method replace : 'k -> 'v -> unit;
 
     class map_ro_assoc ['k, 'v] ?tival tikey cur : map_ro ['k, 'v]
     =
-      object (_self)
+      object (self)
         method tikey = (tikey :> ti 'k);
         method tival = Option.default (tival :> option (ti 'v)) ti_no;
         value keq = tikey#eq;
         method get_exn k = A.get_exn ~keq k cur;
         method get_opt k = A.get_opt ~keq k cur;
+        method get_def k defv = try self#get_exn k with [_ -> defv];
         method get_all k = A.get_all ~keq k cur;
         method mem k = A.mem ~keq k cur;
         method fold
             value get_opt cur k =
               try Some (get_exn cur k)
               with [Not_found -> None];
+            value get_def cur k defv =
+              try get_exn cur k
+              with [Not_found -> defv];
             value mem cur k = M.mem k cur;
             value replace cur k v = M.add k v cur;
             value remove cur k = M.remove k cur;
             method tival = Option.default (tival :> option (ti 'v)) ti_no;
             method get_exn k = Cd_tree.get_exn cur k;
             method get_opt k = Cd_tree.get_opt cur k;
+            method get_def k defv = Cd_tree.get_def cur k defv;
             method get_all k = try [self#get_exn k] with [Not_found -> []];
             method mem k = Cd_tree.mem cur k;
             method fold
 
         method get_exn k = cur#get_exn k;
         method get_opt k = cur#get_opt k;
+        method get_def k defv = cur#get_def k defv;
         method mem k = cur#mem k;
 
         method remove k =
 
         method get_exn k = cur#get_exn k;
         method get_opt k = cur#get_opt k;
+        method get_def k defv = cur#get_def k defv;
         method mem k = cur#mem k;
 
         method remove k =