Ocamlformat and infix operators

I’ve been looking at switching to ocamlformat and I really like it. There is one case that I so far have not been able to get how I like. I do realize that ocamlformat is not trying to be a one-size fits all but it seems to almost have what I want. The case is for infix monadic operators such as:

      Fut.Promise.set t.closed_promise ()
      >>= fun () -> Fut.Promise.set t.recv_promise () >>| fun () -> ()

I would like this to always break as one of the two options below

      Fut.Promise.set t.closed_promise ()
      >>= fun () -> 
      Fut.Promise.set t.recv_promise () 
      >>| fun () -> 

or (less preferably)

      Fut.Promise.set t.closed_promise ()
      >>= fun () -> Fut.Promise.set t.recv_promise () 
      >>| fun () -> ()

If this is a reasonable request, perhaps break-infix-before-func could be set to always?

Or perhaps I’m missing something in the docs.

I apologize because this is off-topic but, have you looked into using let-operators for these operations? It would solve the formatting issue because the code would just look (almost) like regular let-bindings.

Eventually it might get there but it’s not a refactoring I’m prioritizing.

I would expect the second style you suggest to be achievable with:

  • break-infix-before-func;
  • break-infix = fit-or-vertical.

Unfortunately, this combination of options doesn’t work as I would expect right now (c.f. https://github.com/ocaml-ppx/ocamlformat/issues/1094).

I suspect that the first option you suggest is not achievable with current OCamlformat. Personally I find it a bit hard to read, but to each their own :slightly_smiling_face:

My main argument for it is that it corresponds to what let’s would look like, at least to me. In that they have the structure:

let v = thing in

And in this case its:

>>= fun v ->

I think the following would actually be closer, but the real think I want is the expression on its own line

thing >>= fun v ->

I completely agree that the final expression should be bumped onto its own line whenever infix operators are kept at the end of lines like that:

thing >>= fun v ->

This is a nice style for interoperability with let+ syntax, and I think would probably be more widely-accepted than the 3-line variant.

I submitted an issue about this very thing a while ago :slightly_smiling_face:


And that being said, I am quite liking ocamlformat and even if I can’t get it quite how I ideally would want my config I’m willing to put up with it to just not have to care.

For anyone interested, here is my config:

align-cases = true
align-constructors-decl = true
align-variants-decl = true
break-cases = fit-or-vertical
break-infix = fit-or-vertical
break-fun-decl = fit-or-vertical
break-fun-sig = fit-or-vertical
break-collection-expressions = fit-or-vertical
break-separators = after
doc-comments = before
doc-comments-val = before
type-decl = sparse
break-infix-before-func = true
if-then-else = k-r
parens-tuple-patterns = always
wrap-fun-args = false
match-indent = 2
match-indent-nested = always
m = 100

It would be easier to keep track of and maybe have better visibility if you were to open an issue at https://github.com/ocaml-ppx/ocamlformat.