// *** before: ***
struct AdderOld {
let base: Int
func call(_ value: Int) -> Int { base + value }
}
let addOld = AdderOld(base: 10)
print(addOld.call(5)) // the name of the method was in the way at every use
// *** in version 5.2: ***
struct Adder {
let base: Int
func callAsFunction(_ value: Int) -> Int { base + value }
}
let add = Adder(base: 10)
print(add(5)) // callAsFunction is chosen by the compiler
// it may be overloaded, throwing, mutating and generic
struct Logger {
var level = 0
mutating func callAsFunction(_ message: String) { level += 1; print(
message) }
func callAsFunction(_ number: Int) { print("number \(number)") }
}
// a stored closure would give the same call, but it cannot be overloaded
// and it is not a member of the type