How to use the module Queue available from Ocaml: OCaml library : Queue
I want to basically store integers in a queue, with add (enqueue), front (dequeue), etc., and avoid writing a Queue by myself.
How to use the module Queue available from Ocaml: OCaml library : Queue
I want to basically store integers in a queue, with add (enqueue), front (dequeue), etc., and avoid writing a Queue by myself.
What have you tried so far? Did you get an error? Or is it not working somehow? If so, what are the expected and actual results?
Ok, I think I figured it out. I was confused on how to create a queue, from the documentation I tried this and thought it was wrong:
let w = Queue.create ()
Then I realized w is initialized as a queue, and subsequently we need to repeatedly call Queue.add 1 w
etc., while I was trying something like w.add
etc.
Thanks,
Gokul
In a general way, the Ocaml standard library never use method. You always get a type named t
(or 'a t
when the type is parametrized) and all the functions in the module apply the this type.
Knowing that, you can read the signature: val fn_name: … -> t -> …
and you have just have to follow the types* in order to use the function from the module.
Can I ask – is this Module.t
thing purely a convention (that a single main module type should labeled t
), or is there anything about the language or compiler that treats a t
differently?
It’s purely a convention.
Thanks for letting me know. I felt it might be useful to have simple usage examples in the documentation.
It’s just a convention in the sense that the compiler does not treat type names in a special way, however using conventions is important for compatibility with certain stdlib (and ecosystem) features. As soon as you do anything that requires a module signature, such as applying a functor for example, then you will need to follow these naming conventions.
Then ppx_deriving
processor generates different code with t
. See GitHub - ocaml-ppx/ppx_deriving: Type-driven code generation for OCaml
However, this specific behavior is not related with the core language.
Thanks to all for the answers!