Last active
June 24, 2019 01:30
-
-
Save piyo7/a82da2bcb5ede5a53d6d93354d7cea6f to your computer and use it in GitHub Desktop.
Scalaで一番よく使うローンパターン ref: https://qiita.com/piyo7/items/c9be1f39bcfea43a778a
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
import java.io.Writer | |
import scala.io.Source | |
object Using { | |
def apply[A, B](resource: A)(process: A => B)(implicit closer: Closer[A]): B = | |
try { | |
process(resource) | |
} finally { | |
closer.close(resource) | |
} | |
} | |
case class Closer[-A](close: A => Unit) | |
object Closer { | |
implicit val sourceCloser: Closer[Source] = Closer(_.close()) | |
implicit val writerCloser: Closer[Writer] = Closer(_.close()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment