Created
April 17, 2012 23:01
-
-
Save jedws/2409715 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
| object Loan { | |
| trait Close[A] { | |
| def close(a: A): Unit | |
| } | |
| object Close { | |
| private def closeQ[A: Close](a: A): Boolean = | |
| try { implicitly[Close[A]].close(a); true } | |
| catch { case _: Exception => false } | |
| implicit def Tuple2Close[A: Close, B: Close] = new Close[(A, B)] { | |
| def close(t: (A, B)) { closeQ(t._1) && closeQ(t._2) } | |
| } | |
| implicit def Tuple3Close[A: Close, B: Close, C: Close] = new Close[(A, B, C)] { | |
| def close(t: (A, B, C)) = { closeQ(t._1) && closeQ(t._2) && closeQ(t._3) } | |
| } | |
| } | |
| type Connection = Unit | |
| type Session = Int | |
| type User = String | |
| implicit val ConnectionClose = new Close[Connection] { def close(c: Connection) {} } | |
| implicit val SessionClose = new Close[Session] { def close(c: Session) {} } | |
| implicit val UserClose = new Close[User] { def close(c: User) {} } | |
| def database: Connection = () | |
| def session = 2 | |
| def user = "Bob" | |
| def withA[A: Close, B](a: A)(f: A => B): B = f(a) | |
| withA((database, session, user)) { | |
| case (db, s, u) => | |
| println("hi") | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment