1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-02 22:58:01 +00:00

Add Making Things Mutable as a reason til

This commit is contained in:
jbranchaud
2018-07-18 22:42:22 -05:00
parent ecb7e0f659
commit 6beb485707
2 changed files with 33 additions and 1 deletions

View File

@@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
For a steady stream of TILs from a variety of rocketeers, checkout
[til.hashrocket.com](https://til.hashrocket.com/).
_690 TILs and counting..._
_691 TILs and counting..._
---
@@ -511,6 +511,7 @@ _690 TILs and counting..._
- [Generate Starter Reason Projects](reason/generate-starter-reason-projects.md)
- [Helping The Compiler Help Us With Variants](reason/helping-the-compiler-help-us-with-variants.md)
- [Inline Component Styles With Reason React](reason/inline-component-styles-with-reason-react.md)
- [Making Things Mutable](reason/making-things-mutable.md)
- [Multi-Argument Functions As Syntactic Sugar](reason/multi-argument-functions-as-syntactic-sugar.md)
- [Pattern Match On Exceptions](reason/pattern-match-on-exceptions.md)
- [Quickly Bootstrap A React App Using Reason](reason/quickly-bootstrap-a-react-app-using-reason.md)

View File

@@ -0,0 +1,31 @@
# Making Things Mutable
In [ReasonML](https://reasonml.github.io/en/), things that we create with
`let` are immutable -- which means that we can't change them.
```reason
let num = 5;
```
Once `num` is _bound_ to `5` it is stuck with that value for the duration
of it's scope.
ReasonML doesn't completely restrict us to immutability though. The
`ref` construct allows us to bind a variable to a sort of box that holds a
value. We can then look in the box and change what is in the box.
```reason
let num = ref(5); /* put 5 in the box */
Js.log(num^); /* use `^` to look in the box */
num := 3; /* remove 5, put 3 in the box */
```
We use `ref` to bind our variable to a box with some initial value. The `:=`
assignment operator allows us to change what's in the box. Anytime we want
to refer to what's in the box, we postfix our variable with `^`.
Also of note: while `list` instances are not mutable, `array` instances are.
[source](https://reasonml.github.io/docs/en/mutation)