Hands on Capture Checking in Scala

6 points by xvw


osa1

Can I not use the def syntax for tracking captures? I tried it on this part of the full code linked in the post:

  // `f` captures `a1`, a tracked, free variable it explicitly refers to in its body.
  val f: Int ->{a1} Int =
    x =>
      println(a1)
      x + 1

If I change this to

  def f2(x: Int): Int^{a1} =
    println(a1)
    x + 1

I get

[error] ./test.scala:275:19
[error] Int is a pure type, it makes no sense to add a capture set to it
[error]   def f2(x: Int): Int^{a1} =
[error]                   ^^^^^^^^

Having to use the val syntax is a bit annoying. Is this going to be fixed?

I’m also confused about how to capture other functions. I tried this:

val f1: () -> Unit =
  () => ()

val f2: () -> Unit =
  () => ()

val f3: () ->{f1, f2} Unit =
  () =>
    f1()
    f2()

Which fails with

[error] ./test.scala:117:15
[error] (f1 : () -> Unit) cannot be tracked since it is not a parameter or local value
[error] val f3: () ->{f1, f2} Unit =
[error]               ^^^^^^

I tried annotating the functions with cap using () ->{cap} Unit and () -> Unit^{cap} but no luck.

Is it possible to track captured top-level functions?