# Opinions on \`+=\` / \`+.=\`?

**URL:** <https://discuss.ocaml.org/t/opinions-on/8569>\
**Category:** Community\
**Created:** [October 4, 2021, 7:38am UTC](https://discuss.ocaml.org/t/opinions-on/8569 "2021-10-04T07:38:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rixed](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/rixed/32/56_2.png) [@rixed](https://discuss.ocaml.org/u/rixed)\
**Post date:** [October 4, 2021, 7:38am UTC](https://discuss.ocaml.org/t/opinions-on/8569/1 "2021-10-04T07:38:32Z")

</div>

I’m sure this came up already but what would you think if the stdlib had a `+=` operator (alias for `incr`) and `+.=` (for floats), and similarly for other arithmetic operators ?

---

<div class="post-metadata">

**Author:** ![cloudyhug](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/cloudyhug/32/2399_2.png) [@cloudyhug](https://discuss.ocaml.org/u/cloudyhug)\
**Post date:** [October 4, 2021, 9:11am UTC](https://discuss.ocaml.org/t/opinions-on/8569/2 "2021-10-04T09:11:41Z")

</div>

Like the following?

```ocaml
let (+=) r x = r := !r + x
let (+.=) r x = r := !r +. x

let i = ref 0
i += 2 (* {contents = 2} *)

```

That would be a nice syntax, but I don’t know if you really want to do computations on integers in an imperative way, considering such a functional language. I would keep references to a limited set of use cases, for example quite complex global variables, like a global config or idk.

---

<div class="post-metadata">

**Author:** ![EmileTrotignon](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/emiletrotignon/32/5913_2.png) [@EmileTrotignon](https://discuss.ocaml.org/u/EmileTrotignon)\
**Post date:** [October 4, 2021, 1:18pm UTC](https://discuss.ocaml.org/t/opinions-on/8569/3 "2021-10-04T13:18:27Z")

</div>

I think it should be there, but its not a big deal if its not, since its so easy to define it yourself.  
And as much as I love functional programming, I also think OCaml should try to present itself much more as an imperative language, because not everyone wants to learn functional, and Ocaml is a very nice imperative language.  
I am even thinking of writing a tutorial aimed at programming beginners, to learn programming starting with Ocaml, that would be focused on imperative stuff.

---

<div class="post-metadata">

**Author:** ![Chet\_Murthy](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chet_murthy/32/1501_2.png) [@Chet\_Murthy](https://discuss.ocaml.org/u/Chet_Murthy)\
**Post date:** [October 4, 2021, 6:57pm UTC](https://discuss.ocaml.org/t/opinions-on/8569/4 "2021-10-04T18:57:09Z")

</div>

Like others, I think this is fine, but I’ll just note that I sometimes muse on how nice it’d be, if we could come up with the equivalent of setf/getf for ocaml. So that you could write `a.(i).(j).[k] += 5`

---

<div class="post-metadata">

**Author:** ![cjr](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/cjr/32/3043_2.png) [@cjr](https://discuss.ocaml.org/u/cjr)\
**Post date:** [October 5, 2021, 2:43am UTC](https://discuss.ocaml.org/t/opinions-on/8569/5 "2021-10-05T02:43:02Z")

</div>

Be careful, or you might inspire someone to write a tutorial about OCaml as an OOP language 😉

More seriously, I would love to see this. OCaml is functional-forward but eminently practical. I think that a tutorial focused around its imperative aspects would be incredibly valuable.

---

<div class="post-metadata">

**Author:** ![hyphenrf](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/hyphenrf/32/2733_2.png) [@hyphenrf](https://discuss.ocaml.org/u/hyphenrf)\
**Post date:** [October 5, 2021, 4:43am UTC](https://discuss.ocaml.org/t/opinions-on/8569/6 "2021-10-05T04:43:35Z")

</div>

currently this syntax is problematic because of [precedence rules](https://ocaml.org/api/Ocaml_operators.html); it will have to be special-cased. For example:

```ocaml
let (+=) r i = r := r + i

let _ =
  let x = ref 0 in
  x += 1 |> succ

```

this doesn’t parse as you’d expect it to.
