File: generalized_accessors.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 (114 lines) | stat: -rw-r--r-- 2,766 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
// RUN: %target-typecheck-verify-swift

struct Read {
  var simpleReadImmutable: Int {
    _read {}
  }

  var redundantRead: Int {
    _read {} // expected-note {{previous definition of 'read' accessor here}}
    _read {} // expected-error {{variable already has a 'read' accessor}}
  }

  var readAndGet: Int {
    _read {} // expected-error {{variable cannot provide both a 'read' accessor and a getter}}
    get {} // expected-note {{getter defined here}}
  }

  var readAndAddress: Int {
    _read {} // expected-note {{'read' accessor defined here}}
    unsafeAddress {} // expected-error {{variable cannot provide both an addressor and a 'read' accessor}}
  }
}

struct ReadModifiable {
  var readAndWillSet: Int {
    _read {}
    willSet {} // expected-error {{'willSet' cannot be provided together with a 'read' accessor}}
  }

  var readAndDidSet: Int {
    _read {}
    didSet {} // expected-error {{'didSet' cannot be provided together with a 'read' accessor}}
  }

  var readAndSet: Int {
    _read {}
    set {}
  }

  var readAndMutableAddress: Int {
    _read {}
    unsafeMutableAddress {}
  }

  var readAndModify: Int {
    _read {}
    _modify {}
  }
}

struct Modify {
  var modifyAlone: Int {
    _modify {} // expected-error {{variable with a 'modify' accessor must also have a getter, addressor, or 'read' accessor}}
  }

  var getAndModify: Int {
    get {}
    _modify {}
  }

  var addressAndModify: Int {
    unsafeAddress {}
    _modify {}
  }

  var readAndModify: Int {
    _read {}
    _modify {}
  }

  var getAndRedundantModify: Int {
    get {}
    _modify {} // expected-note {{previous definition of 'modify' accessor here}}
    _modify {} // expected-error {{variable already has a 'modify' accessor}}
  }

  var getAndModifyAndMutableAddress: Int {
    get {}
    _modify {} // expected-note {{'modify' accessor defined here}}
    unsafeMutableAddress {} // expected-error {{variable cannot provide both a mutable addressor and a 'modify' accessor}}
  }

  var getAndModifyAndSet: Int {
    get {}
    _modify {}
    set {}
  }

  var getAndNonMutatingModifyAndNonMutatingSet: Int {
    get {}
    nonmutating _modify {}
    nonmutating set {}
  }

  var getAndNonMutatingModifyAndSet: Int {
    get {}
    nonmutating _modify {} // expected-error {{'modify' accessor cannot be 'nonmutating' when the setter is 'mutating'}}
    set {} // expected-note {{setter defined here}}
  }

  var getAndModifyAndNonMutatingSet: Int {
    get {}
    _modify {}// expected-error {{'modify' accessor cannot be 'mutating' when the setter is 'nonmutating'}}
    nonmutating set {} // expected-note {{setter defined here}}
  }
}

struct ImplicitlyUnwrapped {
  var x: Int!
  var y: Int? {
    _read { yield x }
    _modify { yield &x }
  }
}