简体   繁体   English

在OCaml中+>是什么意思?

[英]What does +> mean in OCaml?

I saw an example of httpaf usage, and it has +> operator-like syntax. 我看到httpaf用法的示例 ,它具有+>类似运算符的语法。 What does it means? 这是什么意思?

let () =
  Command.async_spec
    ~summary:"Start a hello world Async server"
    Command.Spec.(empty +>
      flag "-p" (optional_with_default 80 int)
        ~doc:"int destination port"
      +>
      flag "-h" (required string)
        ~doc:"string destination host"
    ) main
|> Command.run

Unfortunately, I can't find it on the OCaml operator lists. 不幸的是,我在OCaml操作员列表中找不到它。

Unfortunately, I can't find it on the OCaml operator lists. 不幸的是,我在OCaml操作员列表中找不到它。

That's because it isn't defined by the language, but is rather an operator defined by a library. 那是因为它不是由语言定义的,而是由库定义的运算符。 OCaml allows for the definition of operators by user code. OCaml允许通过用户代码定义运算符。 You will need to consult the documentation for the library that defines the operator to learn what it does. 您将需要查阅定义操作员的库的文档,以了解其功能。

As Jeffrey explained in the comments it is an infix function. 正如Jeffrey在评论中解释的那样,它是一个infix函数。 You could rewrite it in prefix manner : 您可以用前缀方式重写它:

empty +> flag "-p" (optional_with_default 80 int) ~doc:"int destination port"
(+>) empty (flag "-p" (optional_with_default 80 int) ~doc:"int destination port")

You can define or even redefine operators as you wish as long as they follow some rules, as the priority and associativity is inherited from the first character, and the characters used must be in that selective list: https://caml.inria.fr/pub/docs/manual-ocaml/expr.html 您可以根据需要定义或重新定义运算符,只要它们遵循某些规则即可,因为优先级和关联性是从第一个字符继承而来的,并且使用的字符必须在该选择列表中: https : //caml.inria.fr /pub/docs/manual-ocaml/expr.html

However, note that if you are interested in the module used there, Command.Spec is deprecated and the new syntax is quite different : 但是,请注意,如果您对在那里使用的模块感兴趣,则不推荐使用 Command.Spec ,并且新语法也大不相同:

Command.Let_syntax(
let%map_open port = flag "-p" (optional_with_default 80 int) ~doc:"int destination port"
         and host = flag "-h" (required string) ~doc:"string destination host"
in main port host)

Read more here : https://ocaml.janestreet.com/ocaml-core/latest/doc/core/Core/Command/ and there : https://dev.realworldocaml.org/command-line-parsing.html 在此处了解更多信息: https : //ocaml.janestreet.com/ocaml-core/latest/doc/core/Core/Command/以及: https : //dev.realworldocaml.org/command-line-parsing.html

I could have made this shorter as a comment but I cannot comment yet, sorry if I went overboard in this answer :) 我可以将其简短地添加为评论,但我还不能发表评论,对不起,如果我在这个答案中过分:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM