# 'mutable' usages

**URL:** <https://discuss.ocaml.org/t/mutable-usages/5215>\
**Category:** Community\
**Created:** [February 25, 2020, 4:59pm UTC](https://discuss.ocaml.org/t/mutable-usages/5215 "2020-02-25T16:59:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![spop](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/spop/32/797_2.png) [@spop](https://discuss.ocaml.org/u/spop)\
**Post date:** [February 25, 2020, 4:59pm UTC](https://discuss.ocaml.org/t/mutable-usages/5215/1 "2020-02-25T16:59:02Z")

</div>

- Question 1: in camllight we can have

```ocaml
type t = A of mutable t ;;

```

How to get an equivalent type in OCaml?

- Question 2: in F# we have

```ocaml
let a = ref 10
a := !a + 1

```

and also

```ocaml
let mutable a = 10 
a <- a + 1

```

Could this syntax be useful in OCaml?

(the \<- operator already exists).

---

<div class="post-metadata">

**Author:** ![nojb](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nojb/32/519_2.png) [@nojb](https://discuss.ocaml.org/u/nojb)\
**Post date:** [February 25, 2020, 5:12pm UTC](https://discuss.ocaml.org/t/mutable-usages/5215/2 "2020-02-25T17:12:09Z")

</div>

> [@spop](#):
>
> - Question 1: in camllight we can have
> 
> ```auto
> type t = A of mutable t ;;
> 
> ```
> 
> How to get an equivalent type in OCaml?

```auto
type t = A of {mutable t: t}

```

Cheers,  
Nicolás

---

<div class="post-metadata">

**Author:** ![threepwood](https://avatars.discourse-cdn.com/v4/letter/t/8dc957/32.png) [@threepwood](https://discuss.ocaml.org/u/threepwood)\
**Post date:** [February 26, 2020, 10:02am UTC](https://discuss.ocaml.org/t/mutable-usages/5215/3 "2020-02-26T10:02:42Z")

</div>

Re: question 1, I am unsure how this could ever be useful. The point of singleton sum types is usually to get a newtype without the hassle of defining a module. So here you appear to want a mutable new type. You can do this with:

```auto
type new_int = { mutable x : int }

```

which will be easier to use than your type as you can use the projection syntax.

I could see this being useful if records were structurally typed (ie if isomorphic records were considered the same type) but in OCaml they are not.

Re: question 2. I am also unsure how this can be useful. What’s the difference between the two in F#? If the difference is that the second one is not boxed, I believe OCaml actually optimizes the first one in such a way when it can.

---

<div class="post-metadata">

**Author:** ![arbipher](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/arbipher/32/3544_2.png) [@arbipher](https://discuss.ocaml.org/u/arbipher)\
**Post date:** [February 26, 2020, 3:43pm UTC](https://discuss.ocaml.org/t/mutable-usages/5215/4 "2020-02-26T15:43:26Z")

</div>

For question 2, [OCaml doc on Impertive Features](https://caml.inria.fr/pub/docs/manual-ocaml/coreexamples.html#s:imperative-features) says in OCaml you can either

1. declared mutable in the definition of the **record type**  
`type mutable_point = { mutable x : float; mutable y : float; }`

2. the standard library provides references, which are mutable indirection cells. however, references are implemented as a single-field mutable record  
`type 'a ref = { mutable contents : 'a; }`

Your second snippet won’t appear in OCaml.
