简体   繁体   中英

What does +> mean in OCaml?

I saw an example of httpaf usage, and it has +> operator-like syntax. 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.

Unfortunately, I can't find it on the OCaml operator lists.

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

However, note that if you are interested in the module used there, Command.Spec is deprecated and the new syntax is quite different :

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

I could have made this shorter as a comment but I cannot comment yet, sorry if I went overboard in this answer :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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