Skip to content

Commit

Permalink
Create some JavaExamples
Browse files Browse the repository at this point in the history
  • Loading branch information
txiang61 authored Apr 26, 2024
1 parent 1a53cbb commit c843a86
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions JavaExamples
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/// Java Examples

// Mutating immut set, `s` is immut
void foo(Set<String> s) {
Set<String> new_s = s;
new_s.add("x"); // ERROR
}

// Mutating immut set, `new_s` is immut
void foo(@Mutable Set<String> s) {
Set<String> new_s = new HashSet<>(s);
new_s.add("x"); // ERROR
}

// Mutating mut set
void foo(Set<String> s) {
@Mutable Set<String> new_s = new HashSet<>(s);
new_s.add("x"); // OK
}

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

// Immut class and its members
class Person {
String name;
List<String> family;

Person(String n, List<String> f) {
this.name = n; // OK
this.family = f; // OK
}

void setName(String n) {
this.name = n; // ERROR
}

@Mutable List<String> getFamily() {
return family; // ERROR
}
}

void foo(Person p) {
p.name = "Jenny"; // ERROR
p.family.add("Jenny"); // ERROR
p.family.getFamily().add("Jenny"); // OK
}

// Mut class and its members
@Mutable class Person {
String name;
List<String> family;

Person(String n, List<String> f) {
this.name = n; // OK
this.family = f; // OK
}

void setName(String n) {
this.name = n; // OK
}

List<String> getFamily() {
return family; // OK
}
}

void foo(Person p) {
p.name = "Jenny"; // OK
p.family.add("Jenny"); // OK
p.family.getFamily().add("Jenny"); // ERROR
}

0 comments on commit c843a86

Please sign in to comment.