Variant constructor name collision

Can I have two constructor from different variant types with a same name but different parameters?
For example:

type a = 
| A

type b = 
| A(int)

Currently the compiler would complain, and I tried using a.A and b.A to differentiate, but seems it’s not a valid syntax.

You need to add the type information for the variable

let x:a = A
let y = A(1)

2 Likes

Oh I see! Actually I’m using it in a pattern matching expression inside a function.

type a = 
| A

type b = 
| A(int)

let f = (item) => switch item {
| A => true
}

So the real problem here is that the compiler cannot infer the correct type of the parameter from the name of the constructor in the first pattern matching clause.
Thanks!

You can also put the two types in separate modules:

module A = {
  type t = A
}

module B = {
  type t = A(int)
}

Then you can prefix the variant constructor with its module name to tell the compiler which one you’re referring to:

let f = item =>
  switch item {
  | A.A => true
  }
2 Likes