Design problem
I'm making a parser generator and have a production type:
type t = {lhs: Symbol.t; rhs: Symbol.t list; properties: ?}
The problem is the type of the properties. The properties are additional information that tags along, it moves from production to items, from items to actions and then to the CST nodes. I want the user to be able to select any type, however if I do:
type 'props t = {lhs: Symbol.t; rhs: Symbol.t list; properties: 'props}
I have to add the type parameter everywhere and then can no longer make a set, because the type is now incompatible with the Set.OrderedType.
I guess I could make every module a functor that accepts the properties type, however I would again have to modify everything. The other option would be to make the properties type existential and pack along the type witness, so I can recover the type later, however that makes the type dynamic for no good reason.
Is there a better solution?
Posted Jul 10, 2020, 11:41 AM UTC · edited
You know the options. Just go for functors. That way the user can have control. Don’t be afraid of “changing everything”. Not wanting to change code leads to bad design. It’s leas work than you think.
Yep! This sounds like a pretty solid use case for functors. And since it's OCaml, the compiler will work with you through the refactoring process to make the whole thing even easier.
I guess functors it is then. I kind of hoped there existed some easier way that I haven't thought of.
I believe that GP is right, functors are the best approach in your situation.
You could try the advanced examples in the first class modules section of the documentation (which also use functors), though it is tricky.