如何解决如何将 OCaml 库引用添加到 Reason 代码文件?
今天刚刚开始使用 Reason 和 OCaml。我从 https://github.com/esy-ocaml/hello-reason 示例开始。我想进行 HTTP API 调用,所以我安装了 ocaml-cohttp:esy add @opam/cohttp-lwt
。
现在我想在 hello-reason 入门示例中使用该库(或任何您可能提出的建议)。
我找不到有关如何导入它的参考文档。我试过了:
open cohttp-lwt
我可以在 Reason 代码文件中使用 OCaml 库吗?
解决方法
是的,唯一的区别是语法。 client tutorial 可以是 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
使用 ocaml-dune,因此您还必须将 cohttp-lwt-unix
添加到项目的 libraries
文件中的 dune
节,如shown in the client tutorial here。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。