Introduce a region context.
The same mutable field in the same region have the same abstract representation.
The concept of regions is intended to make context-sensitivity tunable for complex use cases.
Example:
trait B { def foo(): Int } class C(var x: Int) extends B { def foo(): Int = 20 } class D(var y: Int) extends B { def foo(): Int = A.m } class Box(var value: B)
object A: val box1: Box = region { new Box(new C(5)) } val box2: Box = region { new Box(new D(10)) } val m: Int = box1.value.foo()
In the above, without the two region annotations, the two objects box1
and box2
are in the same region. Therefore, the field box1.value
and box2.value
points to both instances of C
and D
. Consequently, the method call box1.value.foo()
will be invalid, because it reaches A.m
, which is not yet initialized. The explicit context annotation solves the problem.