如何解决OCaml中'a Conduit_async.io'类型的预期?

时间:2017-07-04 08:35:31

标签: ocaml

我想通过cohttp获取http响应的标题

open Core
open Async
open Cohttp
open Cohttp_async

let cli_hdr url =
  let uri = Uri.of_string url in
  let%bind resp_head = Cohttp_async.Client.head uri in
  resp_head |> Response.headers |> Header.to_string >>| fun hdrs ->
  print_endline hdrs

let () =
  Command.async_basic
    ~summary:"Retrieve definitions from dudugo search engine"
    Command.Spec.(
      empty
      +> anon ("link" %: string)
    )
    (fun link () -> cli_hdr link)
  |> Command.run

但编译编程时出现类型错误:

$ corebuild -pkg async,cohttp,cohttp.async test.native
+ ocamlfind ocamlc -c -w A-4-33-40-41-42-43-34-44 -strict-sequence -g -bin-annot -short-paths -thread -package async,cohttp,cohttp.async -package core -ppx 'ppx-jane -as-ppx' -o test.cmo test.ml
File "test.ml", line 17, characters 2-51:
Error: This expression has type string but an expression was expected of type 'a Conduit_async.io
Command exited with code 2.

我想知道如何让它发挥作用?

1 个答案:

答案 0 :(得分:2)

使用let%map代替let%bind,如下所示

let cli_hdr url =
  let uri = Uri.of_string url in
  let%map resp_head = Cohttp_async.Client.head uri in
  resp_head
  |> Response.headers
  |> Header.to_string
  |> print_endline