Created
August 25, 2016 10:50
-
-
Save 13h3r/b815a016c41cdee933ec5799d1920120 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 ru.dgis.casino.advisor | |
| //**** | |
| case class JdbcConnection() { | |
| def closeJdbc() = println("closing jdbc") | |
| } | |
| trait TTT | |
| object TTT1 extends TTT | |
| object TTT2 extends TTT | |
| case class FileConnection() { | |
| def closeJdbc(t: TTT) = println("closing file") | |
| } | |
| //**** | |
| //**** | |
| trait Closeble[T] { | |
| def close(t: T): Unit | |
| } | |
| object Closeble { | |
| implicit object X extends Closeble[JdbcConnection] { | |
| override def close(c: JdbcConnection): Unit = { | |
| c.closeJdbc() | |
| } | |
| } | |
| implicit object YX extends Closeble[FileConnection] { | |
| override def close(c: FileConnection): Unit = { | |
| c.closeJdbc(TTT1) | |
| } | |
| } | |
| implicit def tupleCloseble[A, B](implicit a: Closeble[A], b: Closeble[B]): Closeble[(A, B)] = { | |
| new Closeble[(A, B)] { | |
| override def close(t: (A, B)): Unit = { | |
| a.close(t._1) | |
| b.close(t._2) | |
| } | |
| } | |
| } | |
| def using[T, Out](t: T)(f: T => Out)(implicit x: Closeble[T]) = { | |
| try { | |
| f(t) | |
| } finally { | |
| x.close(t) | |
| } | |
| } | |
| } | |
| //**** | |
| object XXX extends App { | |
| import Closeble._ | |
| using(new JdbcConnection, new FileConnection) { case (jdbc1, file1) => | |
| println("SSSS") | |
| } | |
| using(new JdbcConnection) { jdbc1 => | |
| using(new JdbcConnection) { jdbc2 => | |
| using(new FileConnection) { f => | |
| println("SSSS") | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment