2017-07-04 53 views
2

我想cohttp如何解决OCaml中'a Conduit_async.io类型的预期问题?

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 

得到HTTP响应的头,但有编制程序时的错误类型:

$ 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. 

我不知道如何使它发挥作用?

+0

不知道这一点,我不知道核心,但在RWO,第18章仰视,我发现近似匹配你的代码片段,但事情是你的代码在'anon'和''link''之间缺少,这是编译器似乎窒息的地方。也许你需要再看看这个章节? – didierc

回答

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