File tree Expand file tree Collapse file tree 5 files changed +65
-3
lines changed
docs/_docs/reference/experimental/capture-checking
tests/neg-custom-args/captures Expand file tree Collapse file tree 5 files changed +65
-3
lines changed Original file line number Diff line number Diff line change @@ -62,7 +62,7 @@ can only access exclusive capabilities defined in the class or passed to it in p
6262For instance, we can also define counter objects that update a shared variable that is external to the object:
6363
6464``` scala
65- object Registry extends Mutable :
65+ object Registry extends Stateful :
6666 var sharedCount = 0
6767 update class CounterX :
6868 update def next : Int =
@@ -87,7 +87,7 @@ an update method or an update class as non-private member or constructor.
8787
8888 1 . The class does not retain any exclusive capabilities from its environment.
8989 2 . The constructor does not take arguments that retain exclusive capabilities.
90- 3 . The class does not does not have fields that retain exclusive universal capabilities.
90+ 3 . The class does not have fields that retain exclusive universal capabilities.
9191
9292** Restriction:** If a class or trait extends ` Stateful ` all its parent classes or traits must either extend ` Stateful ` or be read-only.
9393
@@ -138,7 +138,7 @@ But it also has two other properties that are explained in the following.
138138
139139## Separate Classes
140140
141- Each time one creates a mutable type one gets a separate fresh object that can be updated independently
141+ Each time one creates a value of a mutable type one gets a separate fresh object that can be updated independently
142142of other objects. This property is expressed by extending the ` Separate ` trait in the ` scala.caps ` object:
143143``` scala
144144trait Separate extends ExclusiveCapability
Original file line number Diff line number Diff line change 669 | update def baz() = 1 // error
77 | ^
88 | Update method baz must be declared in a class extending the `Mutable` trait.
9+ -- Error: tests/neg-custom-args/captures/mut-outside-mutable.scala:14:15 -----------------------------------------------
10+ 14 | update def baz() = 1 // error
11+ | ^
12+ | Update method baz must be declared in a class extending the `Mutable` trait.
Original file line number Diff line number Diff line change @@ -8,3 +8,7 @@ trait Foo extends Mutable:
88 def bar =
99 update def baz () = 1 // error
1010 baz()
11+
12+ trait Bar extends Mutable :
13+ class Baz :
14+ update def baz () = 1 // error
Original file line number Diff line number Diff line change 1+ -- [E007] Type Mismatch Error: tests/neg-custom-args/captures/ref-with-file.scala:15:12 --------------------------------
2+ 15 | withFile: f => // error
3+ | ^
4+ |Found: (f: File^'s1) ->'s2 Ref[File^'s3]^
5+ |Required: (f: File^²) => Ref[File^'s4]^'s5
6+ |
7+ |Note that capability cap cannot be included in capture set {} of value r.
8+ |
9+ |where: => refers to a fresh root capability created in method Test when checking argument to parameter op of method withFile
10+ | ^ refers to a fresh root capability classified as Unscoped in the type of value r
11+ | ^² refers to the universal root capability
12+ | cap is a fresh root capability created in anonymous function of type (f: File^'s1): Ref[File^'s3]^ of parameter parameter f² of method $anonfun
13+ 16 | val r = Ref(f)
14+ 17 | r
15+ |
16+ | longer explanation available when compiling with `-explain`
17+ -- [E007] Type Mismatch Error: tests/neg-custom-args/captures/ref-with-file.scala:18:12 --------------------------------
18+ 18 | withFile: f => // error
19+ | ^
20+ |Found: (f: File^'s6) ->'s7 (??? : -> Nothing)
21+ |Required: (f: File^) => Nothing
22+ |
23+ |Note that capability cap cannot be included in capture set {} of value r.
24+ |
25+ |where: => refers to a fresh root capability created in method Test when checking argument to parameter op of method withFile
26+ | ^ refers to the universal root capability
27+ | cap is a fresh root capability created in anonymous function of type (f: File^'s6): (??? : -> Nothing) of parameter parameter f² of method $anonfun
28+ 19 | val r = Ref(f)
29+ 20 | ???
30+ |
31+ | longer explanation available when compiling with `-explain`
Original file line number Diff line number Diff line change 1+ import caps .*
2+
3+ class Ref [T ](init : T ) extends Mutable , Unscoped :
4+ var x = init
5+ def get : T = x
6+ update def put (y : T ): Unit = x = y
7+
8+ class File :
9+ def read (): String = ???
10+
11+ def withFile [T ](op : (f : File ^ ) => T ): T =
12+ op(new File )
13+
14+ def Test =
15+ withFile : f => // error
16+ val r = Ref (f)
17+ r
18+ withFile : f => // error
19+ val r = Ref (f)
20+ ???
21+
22+
23+
You can’t perform that action at this time.
0 commit comments