Yes, hash table entries are limited to one type for each table. This is really a question about a system like OCaml, not a hash table. If it seems strange to require things to be of the same type in a hash table, what about the list?
Without knowing the problem you are solving, it is difficult to understand what to offer. However, the general task is to create an algebraic type that has one option for each of the types you are dealing with:
type alg = A of int | B of float
A value of type (string, alg) Hashtbl.t will store int and floats, using a string as the search key.
# let ht = Hashtbl.create 44;; val ht : ('_a, '_b) Hashtbl.t = <abstr> # Hashtbl.add ht "yes" (A 3);; - : unit = () # Hashtbl.add ht "no" (B 1.7);; - : unit = () # ht;; - : (string, alg) Hashtbl.t = <abstr> # Hashtbl.find ht "yes";; - : alg = A 3
Once you get used to the flexible and strong typing of OCaml, it's hard to get back to systems without it.
source share