Skip to content

Commit bf4cda3

Browse files
isidorebrianberzins
andcommitted
dt - documentation for Mutable<T>
Co-Authored-By: brianberzins <[email protected]>
1 parent 21fe762 commit bf4cda3

File tree

4 files changed

+114
-0
lines changed

4 files changed

+114
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
adding event as Brian
2+
rsvping as Mr. Brian
3+
booking hotel as Steve
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.lambda.utils;
2+
3+
import org.approvaltests.Approvals;
4+
import org.junit.jupiter.api.Test;
5+
import org.lambda.functions.Function0;
6+
7+
public class MutableTest {
8+
9+
@Test
10+
public void exampleOfSingleElementArray() {
11+
// snippet: single_element_array
12+
final int[] i = {1};
13+
Function0<Integer> counter = () -> i[0]++;
14+
// end-snippet
15+
}
16+
17+
@Test
18+
public void exampleOfMutable() {
19+
// snippet: mutable_example
20+
Mutable<String> i = new Mutable<>("Brian");
21+
Scheduler scheduler = new Scheduler(() -> i.get());
22+
scheduler.addEvent();
23+
i.update(n -> "Mr. " + n);
24+
scheduler.rsvp();
25+
i.set("Steve");
26+
scheduler.bookHotel();
27+
// end-snippet
28+
Approvals.verify(scheduler);
29+
}
30+
31+
32+
private class Scheduler {
33+
private Function0<String> namer;
34+
private String log = "";
35+
public Scheduler(Function0<String> namer) {
36+
this.namer = namer;
37+
}
38+
39+
public void rsvp() {
40+
log += "rsvping as " + namer.call() + "\n";
41+
}
42+
43+
public void addEvent() {
44+
log += "adding event as " + namer.call() + "\n";
45+
}
46+
47+
public void bookHotel() {
48+
log += "booking hotel as " + namer.call() + "\n";
49+
}
50+
51+
@Override
52+
public String toString() {
53+
return log;
54+
}
55+
}
56+
}

approvaltests-util/docs/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ A set of convenience functions to make life easier.
2020
* Working with [Checked Exceptions](reference/RuntimeExceptions.md#top): tools to hide them.
2121
* [SimpleLogger](reference/SimpleLogger.md#top)
2222

23+
## Lambdas
24+
25+
* [Mutable](how_to/UseMutablesInLambdas.md)
26+
2327
---
2428

2529
[Back to User Guide](README.md#top)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<a id="top"></a>
2+
3+
# Use to use mutable variables with lamdbas
4+
5+
<!-- toc -->
6+
## Contents
7+
8+
* [Why use logs?](#why-use-logs)
9+
* [Capturing logs in test](#capturing-logs-in-test)
10+
* [See also](#see-also)<!-- endToc -->
11+
12+
13+
## The problem
14+
15+
If you are using a value that changes in a lambda, Java will not compile.
16+
17+
For example:
18+
```java
19+
int i = 1;
20+
Function0<Integer> counter = () -> i++;
21+
```
22+
Will give a compilation error:
23+
24+
```
25+
Variable used in lambda expression should be final or effectively final
26+
```
27+
28+
Despite Java wanting the expressions to be final, the truth is that the objects themselves can have mutable state. This means that the workaround is to put the mutable state inside an effectively final object.
29+
30+
`Mutable<T>` is a solution to this problem.
31+
32+
## Single Element array solution
33+
34+
The most common solution to this is to use a single element array. While this works, we find it to be ugly.
35+
36+
For example:
37+
38+
snippet: single_element_array
39+
40+
## Mutable<T> solution
41+
42+
Mutable allows for easy getting/setting/updating.
43+
44+
For example:
45+
46+
snippet: mutable_example
47+
48+
Will produce the following:
49+
50+
snippet: MutableTest.exampleOfMutable.approved.txt
51+

0 commit comments

Comments
 (0)