I have define two record types as follow:
(* in module A*)
type reg = {name: string; mutable value: Big_int.big_int}
type exp = Reg of reg | Other
(* in module B*)
type abstr = Top | Bot | Elt of int
type register = {name: string; mutable value: abstr}
In module B, I have a list, that I call l, of exp and I’m doing a patter matching on it. So I have something like this:
List.fold_left (fun l elt ->
let str =
match elt with
| A.Reg r -> r.name
| _ -> failwith "exception" in
l@[{name = str; value = Bot}]) [] l
But I get the following error: the expression has type A.reg but was expected of type register. It appears the definition of in module A is hidden by the one in module B. If yes, why is it like this? Can someone explain?
To use a record field name from a module, you need to type:
In your case it would be:
I think Deokhwan Kim is also right, you want
A.Regrather thanA.reg.In answer to “why is it like this”: the tradoff for getting (wonderful and fantastic) type inference is that the compiler can’t make deductions about the meaning of overloaded names from the types of things. The inferences go the other way (in essence). So you can’t distinguish between the two fields named
namebased on types, you need to be explicit about which one you mean. A similar limitation shows up in the distinction between integer and floating arithmetic operators.