Skip to content

Instantly share code, notes, and snippets.

@mbloms
Last active October 31, 2020 15:30
Show Gist options
  • Save mbloms/877724f244752705c5336b368645d126 to your computer and use it in GitHub Desktop.
Save mbloms/877724f244752705c5336b368645d126 to your computer and use it in GitHub Desktop.
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