# Ocamlformat and infix operators

**URL:** <https://discuss.ocaml.org/t/ocamlformat-and-infix-operators/5647>\
**Category:** Ecosystem\
**Tags:** ocamlformat\
**Created:** [April 28, 2020, 6:33pm UTC](https://discuss.ocaml.org/t/ocamlformat-and-infix-operators/5647 "2020-04-28T18:33:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![orbitz](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/orbitz/32/110_2.png) [@orbitz](https://discuss.ocaml.org/u/orbitz)\
**Post date:** [April 28, 2020, 6:33pm UTC](https://discuss.ocaml.org/t/ocamlformat-and-infix-operators/5647/1 "2020-04-28T18:33:01Z")

</div>

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:

```auto
      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

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

```

or (less preferably)

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

---

<div class="post-metadata">

**Author:** ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)\
**Post date:** [April 28, 2020, 6:37pm UTC](https://discuss.ocaml.org/t/ocamlformat-and-infix-operators/5647/2 "2020-04-28T18:37:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![orbitz](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/orbitz/32/110_2.png) [@orbitz](https://discuss.ocaml.org/u/orbitz)\
**Post date:** [April 28, 2020, 6:50pm UTC](https://discuss.ocaml.org/t/ocamlformat-and-infix-operators/5647/3 "2020-04-28T18:50:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![CraigFe](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/craigfe/32/1989_2.png) [@CraigFe](https://discuss.ocaml.org/u/CraigFe)\
**Post date:** [April 29, 2020, 7:55am UTC](https://discuss.ocaml.org/t/ocamlformat-and-infix-operators/5647/4 "2020-04-29T07:55:20Z")

</div>

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](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 🙂

---

<div class="post-metadata">

**Author:** ![orbitz](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/orbitz/32/110_2.png) [@orbitz](https://discuss.ocaml.org/u/orbitz)\
**Post date:** [April 29, 2020, 8:53am UTC](https://discuss.ocaml.org/t/ocamlformat-and-infix-operators/5647/5 "2020-04-29T08:53:26Z")

</div>

> [@CraigFe](#):
>
> 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 🙂

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:

```auto
let v = thing in
expression

```

And in this case its:

```auto
thing
>>= fun v ->
expression

```

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

```auto
thing >>= fun v ->
expression

```

---

<div class="post-metadata">

**Author:** ![CraigFe](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/craigfe/32/1989_2.png) [@CraigFe](https://discuss.ocaml.org/u/CraigFe)\
**Post date:** [April 29, 2020, 8:58am UTC](https://discuss.ocaml.org/t/ocamlformat-and-infix-operators/5647/6 "2020-04-29T08:58:56Z")

</div>

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:

```ocaml
thing >>= fun v ->
expression

```

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](https://github.com/ocaml-ppx/ocamlformat/issues/993) about this very thing a while ago 🙂

---

<div class="post-metadata">

**Author:** ![orbitz](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/orbitz/32/110_2.png) [@orbitz](https://discuss.ocaml.org/u/orbitz)\
**Post date:** [April 29, 2020, 9:00am UTC](https://discuss.ocaml.org/t/ocamlformat-and-infix-operators/5647/7 "2020-04-29T09:00:02Z")

</div>

Thanks!

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:

```auto
version=0.14.1
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

```

---

<div class="post-metadata">

**Author:** ![jjb](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/jjb/32/2713_2.png) [@jjb](https://discuss.ocaml.org/u/jjb)\
**Post date:** [May 1, 2020, 4:05pm UTC](https://discuss.ocaml.org/t/ocamlformat-and-infix-operators/5647/8 "2020-05-01T16:05:15Z")

</div>

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](https://github.com/ocaml-ppx/ocamlformat).
