def square(a: Int) = a * a
println("square(2):" + square(2))
println(1 + 1)
println(1. + 1)
class IntAdder(var value: Int) {
def + (aValue: Int): Int = {
value += aValue
value
}
}
println(new IntAdder(2) + 2)
object Closer { def using(closeable: { def close(): Unit }, f: => Unit) { try { f } finally { closeable.close } } }
class Connection {
def close() {
}
def preClose() {
}
}
class App {
def close() {
}
def authenticate() {
}
}
object Test {
def main(args: Array[String]) {
val connection = new Connection
val app = new App
Closer.using(connection, connection.preClose)
Closer.using(app, app.authenticate)
}
}
val numbers = List(1, 2, 3, 4)
numbers.map(_ * 2) // _ represents every members in List
numbers.map((i: Int) => i * 2) // actually define a anonymous function
def isEven((i: Int): Boolean = i % 2 == 0
numbers.filter(isEven _)
def multiply(left: Int)(right: Int) = left * right
val multiply10 = multiply(10)(_)
multiply10(10)
val right: Int = 10
def multiply(left: Int) = left * right
multiply(10)