Returning an object with additional fields

No, I don’t want to live completely in the module world. Currently, I see that extensible records (and by extensible, I really mean avoiding to repeat re-defining the same fields for largely overlapping records) are only approachable with through the use of objects and classes. And it is unclear to me why the objects are apparently so disliked within important segments of the OCaml community. The only workaround the objects that I see is indeed to use records and to generate code. The question is why generate essentially a certain portion of object and class functionality, when such functionality is present?

My attempt at considering first-class modules as records is actually an attempt to solve the extensibility problem, that with objects and classes I could solve through inheritance at once. So far, I don’t see how such approach could be made workable.

So, the only cost of using objects currently is the cost associated with performance, under currently available compilation scheme? Is that correct?

If so, then I wonder if advanced options, like flambda or flambda-2 might actually inline all method calls that are known statically? If yes, then objects would be fantastically useful and efficient.

While working on this problem, I’m little by little starting to work in a way that pretty well matches the visitors paradigm, that @lukstafi mentioned here. Basically, I define classes that contain attributes and interconnected properties in a relatively dynamic fashion, and that are used as a layer for interacting with larger data structures defining relationships between essential game entities. This approach is turning out to be fantastically useful. Given that, I don’t understand what appears as a condemnation that objects and classes receive within the aforementioned segments of the OCaml community. I don’t see how these problems could have been solved without objects or without unreasonable amount of code generation (all without the semantic benefits of object system).

1 Like