Skip to content

Instantly share code, notes, and snippets.

@jonvuri
Last active March 9, 2016 15:37
Show Gist options
  • Save jonvuri/32f8abff5f5bf4e31383 to your computer and use it in GitHub Desktop.
Save jonvuri/32f8abff5f5bf4e31383 to your computer and use it in GitHub Desktop.
How to complete?
/***
scalaVersion := "2.11.7"
libraryDependencies += "com.jsuereth" %% "scala-arm" % "1.4"
resolvers += "Akka Repository" at "http://repo.akka.io/releases/"
*/
import resource._
object Main extends App {
class DestinationRes {
def init() {
println("DestinationRes init - O")
}
def touch(c: SourceRes) {
c.ping()
}
def close(my: DestinationRes) {
println("DestinationRes close - X")
}
def logout() {
println("DestinationRes logout - O")
}
}
class SourceRes(private val id: String) {
def init() {
println(s"SourceRes init: $id")
}
def ping() {
println(s"SourceRes ping: $id")
}
def close(cw: SourceRes) {
println(s"SourceRes close: $id")
}
}
class SourceResProvider(private val id: String) {
def init() {
println(s"SourceResProvider init: $id")
}
def get(): SourceRes = {
val kid = new SourceRes(s"provided by $id")
kid.init()
kid
}
def close(bcw: SourceResProvider) {
println(s"SourceResProvider close: $id")
}
}
val makeWrapped = () => {
val inner = new SourceResProvider("inner")
inner.init()
managed(inner) map {
bc => managed(bc.get())
}
}
implicit def myDestResource = new Resource[DestinationRes] {
override def close(my: DestinationRes) = my.logout()
}
val wrapped = false
val myDest = new DestinationRes
myDest.init()
for {
dest <- managed(myDest)
source: ManagedResource[SourceRes] <- if (wrapped) makeWrapped() else managed(new SourceRes("bare"))
} {
source map {
s =>
dest.touch(s)
}
}
println("end")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment