如何将 OCaml 库引用添加到 Reason 代码文件?

How do I add an OCaml library reference to a Reason code file?

今天刚开始学习 Reason 和 OCaml。我从 https://github.com/esy-ocaml/hello-reason sample. I want to make a HTTP API call so I've installed ocaml-cohttp 开始:esy add @opam/cohttp-lwt.

现在我想在 hello-reason 入门示例中使用该库(或您可能有的任何建议)。

我找不到有关如何导入它的参考文档。我试过:

open cohttp-lwt

我可以在 Reason 代码文件中使用 OCaml 库吗?

是的,唯一的区别是语法。把client tutorial can be translated directly, and automatically改成这样:

open Lwt;
open Cohttp;
open Cohttp_lwt_unix;

let body =
  Client.get(Uri.of_string("https://www.reddit.com/"))
  >>= (
    ((resp, body)) => {
      let code = resp |> Response.status |> Code.code_of_status;
      Printf.printf("Response code: %d\n", code);
      Printf.printf(
        "Headers: %s\n",
        resp |> Response.headers |> Header.to_string,
      );
      body
      |> Cohttp_lwt.Body.to_string
      >|= (
        body => {
          Printf.printf("Body of length: %d\n", String.length(body));
          body;
        }
      );
    }
  );

let () = {
  let body = Lwt_main.run(body);
  print_endline("Received body\n" ++ body);
};

编辑:hello-reason 使用 , so you also have to add cohttp-lwt-unix to the libraries stanza in the project's dune file, as shown in the client tutorial here.