ocaml-ppx / ppx_deriving_yojson

A Yojson codec generator for OCaml.
MIT License
157 stars 46 forks source link

Unbound value $MYTYPE_to_yojson #151

Closed Murphy-OrangeMud closed 2 years ago

Murphy-OrangeMud commented 2 years ago

It works in utop but fails to build. my file structure

image

my code in file keywordcount.ml:

open Yojson

type kc = {keyword:string; count:int} [@@deriving yojson]
type kcl = kc list [@@deriving yojson]

let to_json tree = 
  kcl_to_yojson (List.fold (Simpledict.assoc_of_dict tree) ~init: [] ~f: (fun init (k,v) -> let obj = {keyword=k; count=v} in obj::init))

my code in src/dune

(library
  (name simpledict)
  (modules
    simpledict)
  (libraries
    core))

(executable
  (name keywordcount)
  (modules
    keywordcount)
  (libraries
    core
    core_unix
    core_unix.sys_unix
    stdio
    simpledict
    yojson
    ppx_deriving_yojson
    ppx_deriving
    ppx_deriving_yojson.runtime))

When I use dune build, there's an error:

dune build
  adding: tests/dune (deflated 16%) 
  adding: tests/tests.ml (deflated 78%)
  adding: src/dune (deflated 59%)
  adding: src/keywordcount.ml (deflated 64%)
  adding: src/simpledict.ml (deflated 71%)
File "src/keywordcount.ml", line 167, characters 2-15:
167 |   kcl_to_yojson (List.fold (Simpledict.assoc_of_dict tree) ~init: [] ~f: (fun init (k,v) -> let obj = {keyword=k; count=v} in obj::init))
        ^^^^^^^^^^^^^
Error: Unbound value kcl_to_yojson
anmonteiro commented 2 years ago

This is not a bug. The dune stanzas are missing a (preprocess ...) specification.

Something like:

(executable ...
  (preprocess (pps ppx_deriving_yojson))
Murphy-OrangeMud commented 2 years ago

This is not a bug. The dune stanzas are missing a (preprocess ...) specification.

Something like:

(executable ...
  (preprocess (pps ppx_deriving_yojson))

Thanks