Hi, I’ve been banging my head on this problem for a while, I feel like I’m trying to solve a puzzle here. It’s for my hobby project so it’s very low importance and if you value your time skip this question.
What I’m trying to do is to write a library similar to THREE.js in rescript (not bindings!)
And I’m trying to do something like inheritance but in a functional rescript way.
In THREE.js there’s an Object3D base class and all other things you can render extend Object3D
class Object3D {
// a bunch of properties and methods like
matrix: Matrix4,
updateWorldMatrix() {}
children: Object3D[]
}
class Mesh extends Object3D {
// and some mesh specific thing
geometry: number[] // simplified of course
}
and then Mesh can access it’s children, matrix, etc. And methods from Object3D work on all the things like Mesh.
So how can I represent something similar in rescript?
I want some base properties and methods that work on all things in the library (like Mesh), I want objects like Mesh to have access to the children (which can be anything that extends Object3D), I want to have an array with different kinds of things that extend Object3D.
Something that I’ve tried
type object3d = { matrix: Matrix4.t, children: NO_IDEA }
type mesh = { geometry: array<float>, object: object3d }
this kind of works, composition is quite nice but then I don’t really know which type to use for children.
Then I’ve tried something like this but then it’s just cyclic dependencies and kind of messy
type object3d = {matrix: Matrix4.t, children: array<node>}
let updateMatrix = (o: object3d) => o
type mesh = {
...object3d,
geometry: array<float>,
}
type camera = {
...object3d,
fov: int,
}
type node =
| Mesh(mesh)
| Camera(camera)
I’m a bit lost, help