如何在简街Ocaml中要求一个模块

时间:2018-10-23 04:03:56

标签: ocaml ocaml-dune

我正在研究http://jsfiddle.net/m5rq6bdj/2/,但对require遇到了问题

#require "re.posix";;

(* open Base ;;
open Core ;; *)

(* type <record-name> =
  {
    <field>: <type>;
    <field>: <type>;
  } *)

  type service_info =
    {
      service_name : string ;
      port : int ;
      protocol : string ;
    }
;;

(* #require "re.postix";; *)

let service_info_of_string line =
  let matches =
    Re.exec (Re.Posix.compile_pat "([a-zA-Z]+)[\t]+([0-9]+)/([a-zA-Z]+)") line
  in
  {
    service_name = Re.get matches 1;
    port = Int.of_string (Re.get matches 2);
    protocol = Re.get matches 3;
  }
;;

print_endline "foobar"

这是我叫dune build的输出

dune build hello_world.exe 
         ppx hello_world.pp.ml (exit 1)
(cd _build/default && .ppx/jbuild/ppx_jane/ppx.exe -o hello_world.pp.ml --impl hello_world.ml --dump-ast)
File "hello_world.ml", line 385, characters 0-1:
Error: Syntax error

这是jbuild

(executable
 ((name hello_world)
 (libraries (core))
 (preprocess (pps (ppx_jane)))))

我发现,如果我使用ocamlc进行编译,则“ #use” topfind“可以使用。但是还不能使其与沙丘一起使用。

感谢小提示

1 个答案:

答案 0 :(得分:0)

以下修复程序将使您的示例生效:

open Core ;; (* in particular, it makes Int visible *)


type service_info =
    {
      service_name : string ;
      port : int ;
      protocol : string ;
    }
;;

(* #require "re.postix";; *)

let service_info_of_string line =
  let matches =
    Re.exec (Re.Posix.compile_pat "([a-zA-Z]+)[\t]+([0-9]+)/([a-zA-Z]+)") line
  in
  {
    service_name = Re.get matches 1;
    port = Int.of_string (Re.get matches 2);
    protocol = Re.get matches 3;
  }
;;

print_endline "foobar"

还有jbuild

(executable
 ((name hello_world)
 (libraries (core re))
 (preprocess (pps (ppx_jane)))))