Hi, I’m trying to find a way to go further with a mini-game project, with rescript version 8.4.2 (using its ocaml syntax 4.06.1).
I reached a point where I need ( / want) to define several kind of enemies (not only one) and I’m wondering how to do so. Using OO would have been just perfect for the task, so that every enemy share the same interface, and the implementations are different and hidden inside each objects.
Unfortunately objects don’t work very well in ReScript 8.4.2. It just doesn’t work if there’s an initialiser, and other elements. I found a work-around which is to define an object with default values, and methods to change these default, in a new_oo_foe
function which has the role to initialise my object.
This is what you can see in directory 3, I define the type type oo_foe
, there is the default object dummy_oo_foe ()
, and the initialiser new_oo_foe
, you can check that this work-around does work, but I’m not really happy with this work-around.
In directory 5 you can see that I came back to a record and which I’m trying use in a fake OO style with a field supposed to be used as a method (see type foe
and its method foe_gun
).
But the problem is that I can change the method implementation, but I would also need to change the fields of the record to match different implementations, which we can do with OO and private variables, but not with a record.
Could also use a variant to join several types in one, but it’s also not as elegant as a real OO.
Is there someone that would have a solution (or work-around) to suggest?