Module-scoped type as alias?

Hello,

Consider the following module type definition:

module type Comparable = {
  type t
  let equal: (t, t) => bool
}

Now, the CmpInt module instance of the module type Comparable:

module CmpInt: Comparable = {
  type t = int
  let equal = (x: int, y: int) => x === y
}

If I write:

let x = CmpInt.equal(1, 1)

I get the compile error:

This has type: int
But this function argument is expecting: CompInt.t

CmpInt.t and int are considered different types. However, I expected CmpInt to be an alias for int.

Can I get CmpInt.t behaving as an alias for int?

Thanks in advance

What you’re actually looking for is this:

module CmpInt: Comparable with type t = int = {
  type t = int
  let equal = (x: int, y: int) => x === y
}

Otherwise the type t is abstract and the compiler can’t know it’s actually int.

5 Likes

Perfect, thanks a lot.

1 Like