Could we have syntax support for defining opaque types?

Right now the only way to define an opaque type is to do so via an interface. eg.

module Length: {
  type t
  let feet: int => t
  let meters: int => t
} = {
  type t = Feet(int) | Meters(int)

  let feet = n => Feet(n)
  let meters = n => Meters(n)
}

What if we could take the lead of Flow’s opaque types and have the ability to mark the type as opaque to anyone outside of the module eg.

module Length = {
  opaque type t = Feet(int) | Meters(int)

  let feet = n => Feet(n)
  let meters = n => Meters(n)
}

Length.t would be opaque everywhere apart from inside of the Length module itself.

10 Likes

Every now and then I’m struggling with the same question. I would like to use them more, but it’s not really convenient to work with.

I tried four different implementations, but there is still just the solution with an interface, right?
An interface file just to hide the implementation detail of a single type :thinking:

I guess this should be considered together with a private-by-default PoC.
Which is mostly (but not entirely) the thing that makes .resi files obsolete.

2 Likes