Hi there, we know that OCaml supports module type. So we may get a integral of value with different type (like A.t
and B.t
even though the t in each module is defined as int
).
My question is, how can we convert a variable from one type to another? Say we have module A
and B
and I want to transform a variable from type A.operand
to B.operand
.
open Core
module A = struct
type operand =
| Imm of Int32.t
end
module B = struct
type operand =
| Imm of Int32.t
end
module C = struct
let transform (op : A.operand) : B.operand = match op with
| Imm i -> i
end
We know that both A and B has type operand
formed of Int32.t
. However, the transform
function will fail to compile with error
16 | | Imm i -> i
^
Error: This expression has type int32 but an expression was expected of type B.operand
There are some type conversion functions like string_of_int
. But how can we define a custom conversion function?