Files
swift-mirror/test/Sema/call_as_function_optional.swift
Dan Zheng f44064cbbc [SE-0253] Introduce callables. (#24299)
Introduce callables: values of types that declare `func callAsFunction`
methods can be called like functions. The call syntax is shorthand for
applying `func callAsFunction` methods.

```swift
struct Adder {
  var base: Int
  func callAsFunction(_ x: Int) -> Int {
    return x + base
  }
}
var adder = Adder(base: 3)
adder(10) // desugars to `adder.callAsFunction(10)`
```

`func callAsFunction` argument labels are required at call sites.
Multiple `func callAsFunction` methods on a single type are supported.
`mutating func callAsFunction` is supported.

SR-11378 tracks improving `callAsFunction` diagnostics.
2019-08-26 23:56:36 -07:00

20 lines
472 B
Swift

// RUN: %target-typecheck-verify-swift
// Test extending `Optional` with `func callAsFunction`.
// `Optional` is an edge case since constraint simplification has special
// `Optional` stripping logic.
extension Optional {
func callAsFunction() -> Optional {
return self
}
func callAsFunction(_ fn: (Int) -> Void) -> Optional {
return self
}
}
func testOptional<T>(_ x: T?) {
_ = x()()()
// Test trailing closure syntax.
_ = x { _ in } ({ _ in })
}