Reputation: 147
I’m working on a project in OCaml using Dune, and I’m encountering an “Unbound value” error for a function defined in a custom module. I’ve ensured that the module is correctly defined and imported, but the error persists.
Relevant files:
lib/markdown.ml
module Markdown = struct
open Omd
let convert_markdown_to_html content =
content |> Omd.of_string |> Omd.to_html
end
lib/lib.ml
module Markdown = Markdown
module Template = Template
module SiteConfig = SiteConfig
module Generator = Generator
lib/generator.ml
open Core
open Core_unix
module Generator = struct
let generate_site config =
let dh = Core_unix.opendir "content/" in
let rec read_all dh acc =
match Core_unix.readdir_opt dh with
| None -> List.rev acc
| Some entry -> read_all dh (entry :: acc)
in
let markdown_files = read_all dh [] in
Core_unix.closedir dh;
List.iter markdown_files ~f:(fun file ->
let input_path = "content/" ^ file in
let output_path = "output/" ^ (Filename.chop_suffix file ".md") ^ ".html" in
let content = In_channel.read_all input_path in
let html_content = Markdown.convert_markdown_to_html content in
let template = In_channel.read_all "templates/default.html" in
let full_content = Template.render_template template html_content in
Out_channel.write_all output_path ~data:full_content
)
end
lib/dune
(library
(name ocamlpress)
(public_name ocamlpress)
(libraries str yojson omd core core_unix))
bin/dune
(executable
(public_name ocamlpress)
(name main)
(libraries ocamlpress omd cohttp lwt cmdliner))
Error Message:
When I run dune build, I get the following error:
File "lib/generator.ml", line 28, characters 25-58:
28 | let html_content = Markdown.convert_markdown_to_html content in
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: Unbound value Markdown.convert_markdown_to_html
I’ve tried cleaning and rebuilding the project using dune clean and dune build, but the error persists. The dune and dune-project files are correctly set up according to Dune documentation. I’m using OCaml version 4.12.0 and Dune version 2.9.1.
Why am I getting the “Unbound value” error for Markdown.convert_markdown_to_html, and how can I resolve this issue?
Upvotes: 0
Views: 98
Reputation: 29106
Files are modules in themselves. Putting a module definition inside a file makes that a submodule of the "file module".
That is, if you have:
(* a.ml *)
module B = struct
let foo = ...
end
From other files you would have to access this as A.B
, since A
is a module defined by the file itself.
Therefore, in generator.ml
you would have to access the function as Markdown.Markdown.convert_markdown_to_html content
. But seeing as the submodule in markdown.ml
seems unnecessary, the better solution is probably to put the function definition at the top level of the file.
Upvotes: 2