Type aliases for extensible types

I was revisiting the shape design thread and I’ve run into something which is not what I would have expected. This is fine:

module Shape = struct
  type t = ..
end
type shape = Shape.t

module Point = struct
  type t = { x : int; y : int }
end
type point = Point.t

type Shape.t += Point of point

But if I replace Shape.t with the type alias shape like this:

type shape += Point of point

I get the error:

Error: Type definition shape is not extensible

Expected behavior ?

1 Like

You need to re-export the extensiveness if you want to add new extension constructors using the shape alias:

type t = ..
type a = t = ..
type a+= A
let is_a (x:t) = function A -> true | _ -> false 
1 Like

Thanks. A bit unexpected, but probably is mentioned in the manual. And…

Now I’m curious if one can do

type a = t = ..

where t is not extensible. Probably not