Ocaml 使用Async发出GET请求
摘自真实世界OCaml书籍的最后一部分,我试图分解给出的示例 我的范围是,只需调用GET并打印我们得到的JSON的一些内容 这是我的代码(应该是给定示例的子集) 我的问题是在编译时出现以下错误:Ocaml 使用Async发出GET请求,ocaml,Ocaml,摘自真实世界OCaml书籍的最后一部分,我试图分解给出的示例 我的范围是,只需调用GET并打印我们得到的JSON的一些内容 这是我的代码(应该是给定示例的子集) 我的问题是在编译时出现以下错误: ocaml setup.ml -build Finished, 0 targets (0 cached) in 00:00:00. + /Users/antouank/.opam/system/bin/ocamlfind ocamlc -c -g -annot -bin-annot -thread -p
ocaml setup.ml -build
Finished, 0 targets (0 cached) in 00:00:00.
+ /Users/antouank/.opam/system/bin/ocamlfind ocamlc -c -g -annot -bin-annot -thread -package yojson -package threads -package textwrap -package re2 -package core -package cohttp.async -I src -o src/main.cmo src/main.ml
File "src/main.ml", line 48, characters 18-41:
Error: This expression has type unit but an expression was expected of type
'a Async.Std.Deferred.t = 'a Async_kernel.Deferred0.t
Command exited with code 2.
Compilation unsuccessful after building 2 targets (0 cached) in 00:00:00.
E: Failure("Command ''/usr/local/bin/ocamlbuild' src/main.native -use-ocamlfind -tag debug' terminated with error code 10")
make: *** [build] Error 1
我如何从延迟中提取字符串,这个错误到底意味着什么?
在这本书中,这个例子是用一个奇怪的命令wrap运行的,所以我看不出如何将它拉出。您对
run
的定义中的问题是匿名函数
fun (word, def) ->
print_endline ("- word: " ^ word);
(
match def with
| None -> print_endline "[EMPTY]"
| Some str -> print_endline str
)
未正确键入以与一元运算符一起使用>=
。它的类型是string*string->unit
,而>=
在这里需要类型为string*string->unit Deferred.t
的函数
如果您在同一章中查看echo服务器的示例,它将建议以下方法:
let run () =
get_definition "OCaml"
>>= fun (word, def) ->
print_endline ("- word: " ^ word);
(
match def with
| None -> print_endline "[EMPTY]"
| Some str -> print_endline str
);
Deferred.return()
let () =
ignore(run ());
never_returns (Scheduler.go ())
谢谢我怀疑我必须a)运行“调度程序”,不管它做什么,b)我必须“忽略返回”,但我不知道如何做。当我运行您给出的代码段时,示例运行良好,但它不会退出。完成后我如何告诉它退出?我在哪里可以找到有关
忽略
和永不返回
的更多信息?
let run () =
get_definition "OCaml"
>>= fun (word, def) ->
print_endline ("- word: " ^ word);
(
match def with
| None -> print_endline "[EMPTY]"
| Some str -> print_endline str
);
Deferred.return()
let () =
ignore(run ());
never_returns (Scheduler.go ())