Skip to content

Commit 10a07e3

Browse files
authored
Update JavaExamples with more examples
1 parent c843a86 commit 10a07e3

1 file changed

Lines changed: 16 additions & 12 deletions

File tree

JavaExamples renamed to JavaExamples.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,43 @@
11
/// Java Examples
2+
import java.util.*;
3+
import qual.Mutable;
24

35
// Mutating immut set, `s` is immut
46
void foo(Set<String> s) {
57
Set<String> new_s = s;
8+
@Mutable Set<String> new_s1 = s; // ERROR
69
new_s.add("x"); // ERROR
710
}
811

912
// Mutating immut set, `new_s` is immut
10-
void foo(@Mutable Set<String> s) {
13+
void foo1(@Mutable Set<String> s) {
1114
Set<String> new_s = new HashSet<>(s);
1215
new_s.add("x"); // ERROR
1316
}
1417

1518
// Mutating mut set
16-
void foo(Set<String> s) {
19+
void foo2(Set<String> s) {
1720
@Mutable Set<String> new_s = new HashSet<>(s);
1821
new_s.add("x"); // OK
1922
}
2023

2124
// Type parameter mutability
22-
void foo(Set<List<String>> s, List<String> l) {
25+
void foo3(Set<List<String>> s, List<String> l) {
2326
assert s.get(l) != null;
2427
List<String> v = s.get(l);
25-
l.add("x"); // ERROR
28+
@Mutable List<String> v2 = s.get(l); // ERROR
2629
v.add("x"); // ERROR
2730
}
2831

29-
// Immut class and its members
32+
// Class and its immut members
3033
class Person {
3134
String name;
3235
List<String> family;
3336

3437
Person(String n, List<String> f) {
3538
this.name = n; // OK
3639
this.family = f; // OK
40+
this.family.add("Mom"); // ERROR
3741
}
3842

3943
void setName(String n) {
@@ -45,20 +49,21 @@ void setName(String n) {
4549
}
4650
}
4751

48-
void foo(Person p) {
52+
void foo4(Person p) {
4953
p.name = "Jenny"; // ERROR
5054
p.family.add("Jenny"); // ERROR
5155
p.family.getFamily().add("Jenny"); // OK
5256
}
5357

54-
// Mut class and its members
55-
@Mutable class Person {
56-
String name;
57-
List<String> family;
58+
// Class and its mut members
59+
class MutPerson {
60+
@Mutable String name;
61+
@Mutable List<String> family;
5862

5963
Person(String n, List<String> f) {
6064
this.name = n; // OK
6165
this.family = f; // OK
66+
this.family.add("Mom"); // OK
6267
}
6368

6469
void setName(String n) {
@@ -70,9 +75,8 @@ List<String> getFamily() {
7075
}
7176
}
7277

73-
void foo(Person p) {
78+
void foo5(MutPerson p) {
7479
p.name = "Jenny"; // OK
7580
p.family.add("Jenny"); // OK
7681
p.family.getFamily().add("Jenny"); // ERROR
7782
}
78-

0 commit comments

Comments
 (0)