Reputation: 419
I am new to OCaml and the types in OCaml really get me confused, in the code below, when I try to have a ref of the colormap type, I got a compile error says the type of res_map is "RegSet.elt Map.Make(Cfg.IGraphNode).t" but colormap is a "RegSet.elt option Map.Make(Cfg.IGraphNode).t" type, Seems to me, the difference is I created a ref of map which has a "key -> value" type, but colormap is a map which has a "key -> value option" type. How can I make a ref of colormap?
module NodeMap = Map.Make(Cfg.IGraphNode)
type colormap = ((RegSet.elt option) NodeMap.t)
let my_func (ig : graph) : colormap =
let res_map = ref NodeMap.empty in
!res_map (*compile error here, type mismatch with colormap*)
Upvotes: 0
Views: 83
Reputation: 66823
It's difficult to try out your code because it's not self-contained. Let's say I add the following definitions:
module Cfg = struct module IGraphNode = String end
module RegSet = struct type elt = int end
type graph = float
Then your code compiles with no errors.
Generally speaking, the way to make a reference to something is just as you have done: ref value
. Of course your code is somewhat nonsensical because it doesn't use the reference for anything afterward, it just dereferences it and returns the referenced value (so the reference itself will disappear). But I assume this is because it's only supposed to be an example.
At any rate, I suspect the problem isn't what you think it is. If you post a self-contained example that shows the problem it will be easier to help.
Upvotes: 2