|
|
|
|
|
by DavidMcLaughlin
5490 days ago
|
|
> One is dotfree and uses braces. One uses a dot. And one is dotfree and uses parens. I feel like it would be a lot better if there were only one way to do this. I don't disagree with the principal of your complaint, coming from working with Perl I know of the potential horrors of having such flexibility. But just to explain why you can have both braces and parens here. In Scala, if you have a single parameter to a function, you can use braces. This is to allow you to create your own DSLs and control structures. The standard example is: def withPrinter(file: File)(op: PrintWriter => Unit) {
val writer = new FileWriter(file)
try {
op(file)
} finally {
writer.close()
}
}
Which for these types of blog posts we could call as: withWriter(new File("test.txt"))(_.println("test"))
But in real applications where function bodies might be more complex, then you can have control abstractions like this: withWriter(new File("test.txt")) {
writer => writer.println("test")
}
So used in a tasteful and sensible way, it's a really nice feature. |
|