Hi, this is probably my misunderstanding of functors, but I could not solve it.
I have a module that defines a type for the dom canvas element, but passing it a canvas leads to type mismatch. Alternatively, I guess it could be due to it being a Js.t object.
A bit simplified:
open Js_of_ocaml
module type Painter = sig
type ctx
type surface
end
module CanvasPainter : Painter = struct
type ctx = Dom_html.canvasRenderingContext2D Js.t
type surface = Dom_html.canvasElement Js.t
end
module MyViewer : GraphViewer = functor
(G: GraphModel)
(P: Painter)
(GP: GraphPainter)
-> (struct
module MyPainter = (GP (G) (P))
type state = {
graph: G.t;
surface: P.surface;
}
let make_graph ns es = (ns, es)
let redraw st = MyPainter.draw st.graph st.layout st.ctx
let make_state g s = {
graph=g;
surface=s;
}
end)
module Viewer = MyViewer (Graph) (CanvasPainter) (GRAPH_PAINTER)
let main =
let graph = Viewer.make_graph [] [] in
let canvas = (Js.Opt.get
(Js.Opt.bind
(Dom_html.document##getElementById (Js.string canvas_id))
Dom_html.CoerceTo.canvas)
(fun _ -> failwith "Could not find canvas"))
in
let viewer_state = (Viewer.make_state
graph
canvas)
in
Viewer.redraw viewer_state
Gives an error (which is in the call to make_state):
File "bin/main.ml", line 54, characters 38-44:
54 | canvas
^^^^^^
Error: The value canvas has type Dom_html.canvasElement Js.t
but an expression was expected of type CanvasPainter.surface
The types seem to align?
I am at a point where functors seem attractive for many things, so also welcoming comments on my use of them.