I’m trying to write a bit of code that builds a list of impure values using a Builder, something like this:
//> using scala 3.nightly
//> using option -language:experimental.captureChecking
import caps.*
class A extends SharedCapability
trait Adder:
def add(a: A): Unit
val builder = List.newBuilder[A]
val adder = new Adder:
override def add(a: A) = builder += a
The compiler is not happy about this:
[error] ./rep.sc:14:39
[error] Found: rep$_.this.A^{a}
[error] Required: rep$_.this.A^'s1
[error]
[error] Note that capability a is not included in capture set {cap}
[error] because (a : rep$_.this.A) in method add is not visible from cap in value builder.
[error]
[error] where: cap is a fresh root capability classified as SharedCapability created in value builder
[error] override def add(a: A) = builder += a
On the one hand, sure: the a declared in add is not visible to builder, I get that.
Instinctively, I’m struggling with it a little because A captures cap and therefore, in my head, adding a new “thing that tracks cap" to builder doesn’t really enlarge its capture set. But I supposed this has to do with cap not being unique?
Is there a way to make this work without using builder += a.unsafeAssumePure (which, yes, that does get the code to compile, but it’s a bit of a cheat)?