Add tags

Add tags to the following comment

Java版(#9162)をScalaに移植しました。 これはこれで味わい深いです。 ありがとうございました。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Java版#9162を移植
import java.io._

case class WithResource[T<:Closeable]( resource:T ) {
    def using[F<:Runnable](f:F) : Unit= {
        try {
            f.run();
        } finally {
            resource.close();
        }
    }
}

object Main{
    def main(args:Array[String]) : Unit = {
        val wr = WithResource(  new PrintWriter(
                                new BufferedWriter(
                                new OutputStreamWriter(System.out))))
        wr.using(new Runnable{
                    override def run():Unit = {
                        wr.resource.print("この文字列はflushしないと書き出されない");
                        wr.resource.print("(closeし忘れは良くある間違い)。");
                    } } )
    }
}

Add tags

The input will be splited to tags with space.

Index

Feed

Other

Link

Pathtraq

loading...