Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions core/src/main/scala/lacasa/Actor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ abstract class Actor[T] {
throw new NoReturnControl
}

/* Variant of `open` for more convenient access to box fields.
*
* `fun` must satisfy the same safety constraints as closures passed to `open`
*/
def update[S](select: => Box[S])(fun: Spore[S, S])(implicit noCapture: Safe[fun.Captured]): Unit = {
val theBox = select
theBox.updateInstance(fun)
}

// swap for accessing fields of type Box[S]
def swap[S](select: => Box[S])(assign: Box[S] => Unit, newBox: Box[S])(
fun: Spore[Packed[S], Unit] { type Excluded = newBox.C })(
Expand Down
9 changes: 7 additions & 2 deletions core/src/main/scala/lacasa/Box.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import scala.reflect.{ClassTag, classTag}
import scala.spores._

import scala.util.control.ControlThrowable

import scala.annotation.unchecked.uncheckedVariance

private class NoReturnControl extends ControlThrowable {
// do not fill in stack trace for efficiency
Expand Down Expand Up @@ -63,7 +63,7 @@ object Safe {
implicit def tuple2IsSafe[T, S](implicit one: Safe[T], two: Safe[S]): Safe[(T, S)] = new Safe[(T, S)] {}
}

sealed class Box[+T] private (private val instance: T) {
sealed class Box[+T] private (private var instance: T @uncheckedVariance) {
self =>

type C
Expand All @@ -77,6 +77,11 @@ sealed class Box[+T] private (private val instance: T) {
}
}

// trusted operation
private[lacasa] def updateInstance(fun: T => T @uncheckedVariance): Unit = {
instance = fun(instance)
}

def open(fun: Spore[T, Unit])(implicit access: CanAccess { type C = self.C }, noCapture: Safe[fun.Captured]): Box[T] = {
fun(instance)
self
Expand Down