Skip to content

Commit

Permalink
Refine the example and add generics example
Browse files Browse the repository at this point in the history
  • Loading branch information
Ao-senXiong committed May 8, 2024
1 parent 0e26006 commit 05471c5
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions JavaExamples.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,14 @@ void foo2(Set<String> s) {
new_s.add("x"); // OK
}

// Type parameter mutability
void foo3(Set<List<String>> s, List<String> l) {
assert s.get(l) != null;
void foo3(List<@Immutable List<String>> s, int l) {
List<String> v = s.get(l);
@Mutable List<String> v2 = s.get(l); // ERROR
v.add("x"); // ERROR
}

void foo4(Person p) {
p.name = "Jenny"; // ERROR, this should be forbid by compiler by adding final before String
p.name = "Jenny"; // Ok, this should be forbid by compiler by adding final before String
p.family.add("Jenny"); // ERROR, can not mutate immut list
}

Expand All @@ -57,7 +55,7 @@ class Person {
}

void setName(String n) {
this.name = n; // ERROR, this should be forbid by compiler by adding final before String
this.name = n; // Ok, this should be forbid by compiler by adding final before String
}

@Mutable List<String> getFamily() {
Expand All @@ -84,3 +82,12 @@ List<String> getFamily() {
return family; // OK
}
}

class ImmutSet<T> {
void foo1 (ImmutSet<MutList<T>> s) { // ERROR
}
void foo2(MutList<ImmutSet<T>> s) { // OK
}
}

@Mutable class MutList<@Mutable T> {}

0 comments on commit 05471c5

Please sign in to comment.