Last active
October 31, 2020 15:30
-
-
Save mbloms/877724f244752705c5336b368645d126 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
package huset | |
sealed class CanMutate { | |
type C | |
} | |
final class ShallowUnique[T >: Null <: AnyRef] private[huset] (private var instance: T) { | |
final def borrow(body: ((x: T) => (CanMutate {type C = x.type } => T))): Unit = { | |
val x = instance | |
given perm as CanMutate {type C = x.type} | |
instance = body(x)(perm) | |
} | |
} | |
object Test { | |
final case class Atom[T](private var instance: T) { | |
self => | |
def get = instance | |
def set(x: T)(using CanMutate {type C = self.type}): Unit = { | |
instance = x | |
} | |
} | |
def main(args: Array[String]): Unit = { | |
val a = new Atom(1) | |
val boxed: ShallowUnique[Atom[Int]] = new ShallowUnique[Atom[Int]](a) | |
println(a) | |
boxed.borrow { atom => { case (perm: CanMutate {type C = atom.type}) => | |
atom.set(2)(using perm) | |
atom | |
}} | |
println(a) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment