Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


Share this Page URL
Help

Chapter 9: Control Abstraction > 9.5 By-name parameters - Pg. 176

C HAPTER 9 · Control Abstraction def withPrintWriter(file: File)(op: PrintWriter => Unit) { val writer = new PrintWriter(file) try { op(writer) } finally { writer.close() } } Listing 9.4 · Using the loan pattern to write to a file. two parameters. Look between the two parameters. In the previous version of withPrintWriter , shown on page 174, you see . . . File, op . . . . But in this version, you see . . . File)(op . . . . Given the above definition, you can call the method with a more pleasing syntax: val file = new File("date.txt") withPrintWriter(file) {