2017-07-04 52 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