File: call_as_function_protocol.swift

package info (click to toggle)
swiftlang 6.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,791,532 kB
  • sloc: cpp: 9,901,743; ansic: 2,201,431; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (62 lines) | stat: -rw-r--r-- 1,322 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// RUN: %target-typecheck-verify-swift

protocol P0 {
  // expected-note @+1 {{protocol requires function 'callAsFunction()' with type '() -> Missing'}}
  func callAsFunction() -> Self
}
func testProtocol(_ x: P0) {
  _ = x()
}
func testGeneric<T : P0>(_ x: T) {
  _ = x()
}

protocol P1 {
  func callAsFunction() -> Self
}
extension P1 {
  // expected-note @+1 {{found this candidate}}
  func callAsFunction() -> Self {
    return self
  }
}
protocol P2 {}
extension P2 {
  // expected-note @+1 {{found this candidate}}
  func callAsFunction(x: Int, y: Int) -> Int {
    return x + y
  }
}

// expected-note@+2 {{add stubs for conformance}}
// expected-error @+1 {{type 'Missing' does not conform to protocol 'P0'}}
struct Missing : P0 {}
struct S0 : P0 {
  @discardableResult
  func callAsFunction() -> S0 { return self }
}
let s0 = S0()
s0()

struct S1 : P1 {
  func callAsFunction() -> S1 { return self }
}

let s1 = S1()
_ = s1()()

struct Conforming : P0 & P1 & P2 {}
let conforming = Conforming()
_ = conforming(x: 1, y: 2)
_ = conforming().callAsFunction(x:y:)(1, 2)
_ = conforming.callAsFunction(x:y:)
_ = conforming.callAsFunction // expected-error {{ambiguous use of 'callAsFunction'}}

protocol P3 {}
extension P3 {
  func callAsFunction() -> Self { return self }
}
struct S3 : P3 {}

let s3 = S3()
_ = s3()()