Let's say that I have a 1 -> n
relation: one todo can has many (or zero) notes and a note can have zero or one todo. How can I achieve this relation in ReasonML? (binding for an external lib)
Here is what I have come with at the moment (which is of course not working)
module Note = {
module Attributes = {
[@bs.deriving abstract]
type t = {
[@bs.optional]
id: float,
[@bs.optional]
text: string,
[@bs.optional]
todo: Todo.Attributes.t,
};
};
};
module Todo = {
[@bs.deriving abstract]
type t = {
[@bs.optional]
id: float,
[@bs.optional]
title: string,
[@bs.optional]
completed: bool,
[@bs.optional]
notes: array(Note.Attributes.t),
};
};
let todo = Todo.Attribute.t(~title="hello");
What if Note and Todo are in one file, an are in separated files?
I am not familliar with Reason but in OCaml, one would do this kind of stuff using mutually recursive module, as in the following minimal example.
Using mutally recursive requires to defines their module type:
module type NoteSig = sig
type t
end
module type TodoSig = sig
type t
end
and the actual modules:
module rec Note : NoteSig = struct
type t = {
todo: Todo.t
}
end
and Todo : TodoSig = struct
type t = {
notes: Note.t array
}
end
If you prefer both module to be in a separate file, you can do pretty much the same thing using functors (still using the module signatures, let's say in a file sig.ml):
a.ml:
module Note (T:Sig.TodoSig) = struct
type t = {
todo: T.t
}
end
b.ml:
module Todo (N:Sig.NoteSig) = struct
type t = {
notes: N.t array
}
end
You can now instanciate your modules in an other file :
c.ml:
module rec NoteImpl = (A.Note(TodoImpl):NoteSig)
and TodoImpl = (B.Todo(NoteImpl):TodoSig)
I can only suppose there is a way to to do the same thing in Reason, probably by adding a lot of brackets everywhere. Hope it helps.