Defining Hashtbl variable

I have been banging my head against a wall trying to figure out how to set a variable to be equal to a hashtable. I’m very new so this is probably a simple fix.

Here’s my function

let foo(_p1: float) (_p2:float): unit = 
    let table = Hashtbl.create 100 in
    (* ... *)
    print_endline "function end"

This doesn’t run without a warning of:
“Error: This expression has type int but an expression was expected of type
(module Base__.Hashtbl_intf.Key with type t = 'a)”

Any help would be greatly appreciated.
Thanks.

1 Like

In base, Hashtabl.create takes a key module as argument, for instance:

let h = Hashtabl.create (module Int)

if you need int keys for instance.

1 Like

Thanks for the response, that runs.

I’m still curious, however, why my solution didn’t work. I was trying to follow the syntax from http://caml.inria.fr/pub/docs/manual-ocaml//libref/Hashtbl.html

Why doesn’t that work? By the way the only module I’m opening at the top of the file is Core. (I tried opening Hashtbl with no success)

Core is a superset of Base which is itself an alternative standard library. The right documentation is here: https://ocaml.janestreet.com/ocaml-core/latest/doc/base/Base/Hashtbl/index.html .

1 Like

Got it, any idea where the reference I posted comes from? Is that from a separate module I’d have to download if I wanted to use it?

That is from the default standard library. In Core, you can access it via the Caml module; that is, as Caml.Hashtbl.

1 Like

To clarify what others have said : Core is a library. You open Core and (I presume) your dune file lists core as a package so when you use Hashtbl it means Core.Hashtbl. You don’t have to use Core though, and if you didn’t Hashtbl would mean Stdlib.Hashtbl. The reference you’re linking is for the standard library, not for Core.

Most code you’ll in examples and discussion will be using standard library modules unless specified otherwise.

When Core is opened if you want to refer to standard library modules you need to use Caml.List etc., though perhaps with recent OCaml Stdlib.List works too.

1 Like