mirror of
https://github.com/jbranchaud/til
synced 2026-01-05 08:08:02 +00:00
Add Making Things Mutable as a reason til
This commit is contained in:
@@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
|
|||||||
For a steady stream of TILs from a variety of rocketeers, checkout
|
For a steady stream of TILs from a variety of rocketeers, checkout
|
||||||
[til.hashrocket.com](https://til.hashrocket.com/).
|
[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)
|
- [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)
|
- [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)
|
- [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)
|
- [Multi-Argument Functions As Syntactic Sugar](reason/multi-argument-functions-as-syntactic-sugar.md)
|
||||||
- [Pattern Match On Exceptions](reason/pattern-match-on-exceptions.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)
|
- [Quickly Bootstrap A React App Using Reason](reason/quickly-bootstrap-a-react-app-using-reason.md)
|
||||||
|
|||||||
31
reason/making-things-mutable.md
Normal file
31
reason/making-things-mutable.md
Normal 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)
|
||||||
Reference in New Issue
Block a user