File: construction.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (123 lines) | stat: -rw-r--r-- 3,209 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// RUN: %target-typecheck-verify-swift

struct S {
  init(i: Int) { }

  struct Inner {
    init(i: Int) { }
  }
}

enum E {
  case X
  case Y(Int)

  init(i: Int) { self = .Y(i) }

  enum Inner {
    case X
    case Y(Int)
  }
}

class C {
  init(i: Int) { } // expected-note 4{{selected non-required initializer 'init(i:)'}}

  required init(d: Double) { }

  class Inner {
    init(i: Int) { }
  }

  static func makeCBad() -> C {
    return self.init(i: 0)
    // expected-error@-1 {{constructing an object of class type 'C' with a metatype value must use a 'required' initializer}}
  }

  static func makeCGood() -> C {
    return self.init(d: 0)
  }

  static func makeSelfBad() -> Self {
    return self.init(i: 0)
    // expected-error@-1 {{constructing an object of class type 'Self' with a metatype value must use a 'required' initializer}}
  }

  static func makeSelfGood() -> Self {
    return self.init(d: 0)
  }

  static func makeSelfImplicitBaseBad() -> Self {
    return .init(i: 0)
    // expected-error@-1 {{constructing an object of class type 'Self' with a metatype value must use a 'required' initializer}}
  }

  static func makeSelfImplicitBaseGood() -> Self {
    return .init(d: 0)
  }
}

final class D {
  init(i: Int) { }
}

// --------------------------------------------------------------------------
// Construction from Type values
// --------------------------------------------------------------------------
func getMetatype<T>(_ m : T.Type) -> T.Type { return m }

// Construction from a struct Type value
func constructStructMetatypeValue() {
  _ = getMetatype(S.self).init(i: 5)
  _ = getMetatype(S.self)(i: 5) // expected-error{{initializing from a metatype value must reference 'init' explicitly}} {{26-26=.init}}
  _ = getMetatype(S.self)
}

// Construction from a struct Type value
func constructEnumMetatypeValue() {
  _ = getMetatype(E.self).init(i: 5)
}

// Construction from a class Type value.
func constructClassMetatypeValue() {
  // Only permitted with a @required constructor.
  _ = getMetatype(C.self).init(d: 1.5) // okay
  _ = getMetatype(C.self).init(i: 5) // expected-error{{constructing an object of class type 'C' with a metatype value must use a 'required' initializer}}
  _ = getMetatype(D.self).init(i: 5)
}

// --------------------------------------------------------------------------
// Construction via archetypes
// --------------------------------------------------------------------------
protocol P {
  init()
}

func constructArchetypeValue<T: P>(_ t: T, tm: T.Type) {
  var t = t
  var t1 = T()
  t = t1
  t1 = t

  var t2 = tm.init()
  t = t2
  t2 = t
}

// --------------------------------------------------------------------------
// Construction via existentials.
// --------------------------------------------------------------------------
protocol P2 {
  init(int: Int)
}

func constructExistentialValue(_ pm: P.Type) {
  _ = pm.init()
  _ = P() // expected-error{{type 'any P' cannot be instantiated}}
}

typealias P1_and_P2 = P & P2
func constructExistentialCompositionValue(_ pm: (P & P2).Type) {
  _ = pm.init(int: 5)
  _ = P1_and_P2(int: 5) // expected-error{{type 'any P1_and_P2' (aka 'any P & P2') cannot be instantiated}}
}