Skip to content

Allow explicit capture of top-level variable #39

Description

@jspenger

The current macro-checks on the explicit capture list rejects top-level variables. We may want to write a program that explicitly captures a top-level variable like the following.

// FutureMap.scala

// ...

object FutureMap {

    val customerData = TrieMap.empty[Int, CustomerInfo]

    // ...

    def averageAge(customers: List[Customer]): Future[Float] = {
        val spore = Spore(customerData){ (cs: Customers) =>
                       // ^^^^^^^^^^^^ explicit capture
            val infos = cs.flatMap { c =>
                customerData.get(c.customerNo) match {
             // ^^^^^^^^^^^^ capture customerData
                    case Some(info) => List(info)
                    case None       => List()
                }
            }
            val sumAges = infos.foldLeft(0)(_ + _.age).toFloat
            if (infos.nonEmpty) sumAges / infos.size else 0.0f
        }
        val safeSpore = duplicate(spore)
                     // ^^^^^^^^^^^^^^^^ duplicate spore with customerData
        Future { safeSpore(customers) }
    }
    // ...
}

It is reasonable to allow the user to explicitly capture things that are used in the body even when the captured thing is accessible on the top-level. And it is useful like in the above example. It is unclear how it should be implemented and how this affects the API. It is still reasonable to give a warning or error if a top-level thing is explicitly listed. Perhaps there should be a separate option/model for capturing top-level variables.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions