Created
July 24, 2016 20:58
-
-
Save phaller/aeb010bd4e0615df2eaa8820bf5449a7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import lacasa.{LaCasaApp, Box, CanAccess} | |
import Box._ // imports `mkBox` | |
import scala.spores._ | |
class Test { | |
var arr: Array[Int] = _ | |
} | |
// stateful class | |
class HasState { | |
var i1 = 5 | |
var i2 = 10 | |
def m(box1: Box[Test], box2: Box[Test]) | |
(implicit a1: CanAccess {type C = box1.C}, a2: CanAccess {type C = box2.C}): Unit = { | |
// 1. mutate own state | |
this.i1 = 6 | |
// 2. open boxes: | |
// capture immutable data and put into boxes | |
// | |
// note that it would not be OK to capture "this"; | |
// however, in this case we're only capturing Ints, which is OK. | |
box1.open(spore { | |
val local1 = this.i1 | |
val local2 = this.i2 | |
(t: Test) => | |
t.arr = Array(local1, local2) | |
}) | |
box2.open({ | |
(t: Test) => t.arr = Array(0, 0, 0) | |
}) | |
} | |
} | |
object Borrow extends LaCasaApp { | |
// expected output: | |
// 6,10 | |
// 0,0,0 | |
def lcMain(args: Array[String]): Unit = { | |
val hs = new HasState | |
mkBox[Test] { packed1 => | |
implicit val acc1 = packed1.access | |
mkBox[Test] { packed2 => | |
implicit val acc2 = packed2.access | |
// pass both boxes to `m`, but don't consume them | |
hs.m(packed1.box, packed2.box) | |
// open boxes to print their contents | |
packed1.box.open({ | |
(t: Test) => println(t.arr.mkString(",")) | |
}) | |
packed2.box.open({ | |
(t: Test) => println(t.arr.mkString(",")) | |
}) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment