Created
January 30, 2014 18:18
-
-
Save suhailshergill/8715180 to your computer and use it in GitHub Desktop.
scala: granular permissions/access restrictions
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
object Foo { | |
// consider the code in each `Foo.scala` object as if it were defined in a | |
// separate file | |
object `Traits.scala` { | |
object Permissions { | |
private[Permissions] sealed trait Permitted[C] { self: C => } | |
private[Permissions] sealed trait Permission { self: Permitted[_] => | |
protected val Permission: Permission | |
} | |
sealed trait ReadOnly extends Permission { | |
self: Permitted[_] => | |
protected implicit val Permission: ReadOnly = ReadOnly | |
} | |
private[Permissions] object ReadOnly extends ReadOnly with Permitted[ReadOnly] | |
trait AnyRead extends ReadOnly with Permitted[Any] | |
sealed trait ReadWrite extends Permission with ReadOnly { | |
self: Permitted[_] => | |
override protected implicit val Permission: ReadWrite = ReadWrite | |
} | |
private[Permissions] object ReadWrite extends ReadWrite with Permitted[ReadWrite] | |
import `Uses.scala`.Write | |
trait UsesWrite extends ReadWrite with Permitted[Write] { self: Write => } | |
} | |
} | |
object `Classes.scala` { | |
import `Traits.scala`.Permissions._ | |
private var _world: String = "World" | |
def world(implicit x: ReadOnly): String = { | |
_world | |
} | |
def world_=(newWorld: String)(implicit x: ReadWrite) = { | |
_world = newWorld | |
} | |
} | |
object `Uses.scala` { | |
import `Traits.scala`._ | |
import `Classes.scala`._ | |
trait Read extends Permissions.AnyRead { | |
protected def getWorld = world | |
} | |
trait Write extends Permissions.UsesWrite { | |
protected def sayHello = { | |
world = s"Hello ${world}" | |
world | |
} | |
} | |
// trait CompilationError { | |
// def getWorld = world | |
// } | |
// trait AnotherCompilationError extends Permissions.UsesWrite { | |
// def getWorld = world | |
// } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment