Ocaml Lwt类型混淆
我不明白为什么Lwt打印函数Ocaml Lwt类型混淆,ocaml,utop,ocaml-lwt,Ocaml,Utop,Ocaml Lwt,我不明白为什么Lwt打印函数Lwt\u io.print有类型 string->unit Lwt.t 但是如果我运行Lwt_io.print“a”>=fun()->Lwt_io.print“b”结果是打印“ab”并返回类型单位 我认为这可能是一个类型错误,因为Lwt_io.print返回的是单位Lwt.t而不是单位。为什么线程的第二部分被称为?我怀疑您会感到困惑,因为utop很聪明 如果你看这个,它是写的 使用lwt或异步库时,UTop将自动等待['a lwt.t]或['a Deferred.t
Lwt\u io.print
有类型
string->unit Lwt.t
但是如果我运行Lwt_io.print“a”>=fun()->Lwt_io.print“b”代码>结果是打印“ab”并返回类型单位
我认为这可能是一个类型错误,因为Lwt_io.print返回的是单位Lwt.t而不是单位。为什么线程的第二部分被称为?我怀疑您会感到困惑,因为utop
很聪明
如果你看这个,它是写的
使用lwt或异步库时,UTop将自动等待['a lwt.t]或['a Deferred.t]值,并返回['a]
这就是为什么
Lwt_io.print "a" >>= fun () -> Lwt_io.print "b";;
似乎属于单元类型
。要查看真实类型,请尝试以下操作
let res = Lwt_io.print "a" >>= fun () -> Lwt_io.print "b";;
#show res;;
您将看到,当您得到您所期望的时,单位Lwt.t
更新:
为了弄清楚类型,我们有
let f = fun () -> Lwt_io.print "b"
val ( >>= ) : 'a Lwt.t -> ('a -> 'b Lwt.t) -> 'b Lwt.t
val print : string -> unit Lwt.t
val f : unit -> unit Lwt.t
Lwt\u io.print“a”
因此返回一个单位Lwt.t
。
这是(>>=)
的第一个参数,因此'a
是单元
。(>>=)
的第二个参数是f
f
需要一个单位
,这是我们所需要的,因为'a
是单位
。它返回一个单位Lwt.t
,因此'b
也是单位
。这意味着最终结果将是单位Lwt.t
我怀疑您会感到困惑,因为utop
很聪明
如果你看这个,它是写的
使用lwt或异步库时,UTop将自动等待['a lwt.t]或['a Deferred.t]值,并返回['a]
这就是为什么
Lwt_io.print "a" >>= fun () -> Lwt_io.print "b";;
似乎属于单元类型
。要查看真实类型,请尝试以下操作
let res = Lwt_io.print "a" >>= fun () -> Lwt_io.print "b";;
#show res;;
您将看到,当您得到您所期望的时,单位Lwt.t
更新:
为了弄清楚类型,我们有
let f = fun () -> Lwt_io.print "b"
val ( >>= ) : 'a Lwt.t -> ('a -> 'b Lwt.t) -> 'b Lwt.t
val print : string -> unit Lwt.t
val f : unit -> unit Lwt.t
Lwt\u io.print“a”
因此返回一个单位Lwt.t
。
这是(>>=)
的第一个参数,因此'a
是单元
。(>>=)
的第二个参数是f
f
需要一个单位
,这是我们所需要的,因为'a
是单位
。它返回一个单位Lwt.t
,因此'b
也是单位
。这意味着最终结果将是一个单位Lwt.t
,因此,如果它确实是类型单位Lwt.t
,为什么Lwt的结果会打印“a”
激活下一个需要单位的函数。我刚刚更新了答案,以提供有关类型的更多详细信息。因此,如果它确实是类型unit Lwt.t
为什么Lwt\u io.print“a”
的结果会激活下一个需要单位的函数。我刚刚更新了答案,以提供有关类型的更多详细信息。