【问题标题】:How do I modify this line of OCaml to not return a function?如何修改这行 OCaml 以不返回函数?
【发布时间】:2019-12-22 21:15:13
【问题描述】:

我试图在不了解 OCaml 的情况下克服此错误以运行命令行实用程序。

$ dune build
File "_none_", line 1:
Warning 58: no cmx file was found in path for module Toploop, and its interface was not compiled with -opaque
File "vendor/notty/lwt/notty_lwt.ml", line 68, characters 25-64:
Error: This expression has type (unit -> unit) Lwt.t
       but an expression was expected of type unit Lwt.t
       Hint: Did you forget to provide `()' as argument?

第 68 行如下所示:

    Lwt.async (fun () -> Lwt_stream.closed stream >|= fun _ -> f);

这是周围的上下文,以防万一:

  let input_stream ~nosig fd stop =
    let `Revert f = setup_tcattr ~nosig (Lwt_unix.unix_file_descr fd) in
    let stream =
      let flt  = Unescape.create ()
      and ibuf = Bytes.create bsize in
      let rec next () =
        match Unescape.next flt with
        | #Unescape.event as r -> Lwt.return_some r
        | `End   -> Lwt.return_none
        | `Await ->
            (Lwt_unix.read fd ibuf 0 bsize <??> stop) >>= function
              | Left n  -> Unescape.input flt ibuf 0 n; next ()
              | Right _ -> Lwt.return_none
      in Lwt_stream.from next in
    Lwt.async (fun () -> Lwt_stream.closed stream >|= fun _ -> f);
    stream

现在,我在 Stack Overflow 上发现了另一个描述相同错误的问题:OCaml: Lwt expression was expected of type unit Lwt.t。这里,答主建议更换

let create_server sock =
  let serve () =
    Lwt_unix.accept sock >>= accept_connection
  in serve (* serve is a function, not a thread *)

let create_server sock =
  Lwt_unix.accept sock >>= accept_connection

那么,如何使用我的第 68 行进行模拟?

如果必须,我会去学习一些 OCaml 和这种让我想起 lambda 的奇怪语法。但我想做的只是安装一个基于终端的游戏计时器,这样我就可以玩我的游戏了-_-'而且我已经在这个兔子洞里走得更远了,我不想再玩这个游戏了。任何帮助将不胜感激。

【问题讨论】:

  • 多么神秘的一行。到目前为止,我发现&gt;|=map 的中缀表示法。那么,也许这条线是......采用匿名函数并将它们包装起来?尝试了一些东西,但没有成功。

标签: ocaml


【解决方案1】:
Lwt.async (fun () -> Lwt_stream.closed stream >|= fun _ -> f ());

【讨论】:

  • 我以为我是随机尝试的。我一定在其他地方有残留的变化,导致了不同的错误。成功了,谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多