Skip to content

Commit 9a90f0a

Browse files
authored
Merge pull request #66731 from DougGregor/observable-willset-didset-5.9
[Macros] Add test using observer accessors (didSet/willSet) with Observable
2 parents b89e4d6 + b720a35 commit 9a90f0a

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// REQUIRES: swift_swift_parser, executable_test
2+
3+
// RUN: %target-run-simple-swift( -Xfrontend -disable-availability-checking -enable-experimental-feature InitAccessors -enable-experimental-feature Macros -Xfrontend -plugin-path -Xfrontend %swift-host-lib-dir/plugins) | %FileCheck %s
4+
5+
// Asserts is required for '-enable-experimental-feature InitAccessors'.
6+
// REQUIRES: asserts
7+
8+
// REQUIRES: observation
9+
// REQUIRES: concurrency
10+
// REQUIRES: objc_interop
11+
// UNSUPPORTED: use_os_stdlib
12+
// UNSUPPORTED: back_deployment_runtime
13+
14+
import _Observation
15+
16+
@Observable
17+
public class Model {
18+
public enum State {
19+
case initializing
20+
case running
21+
case complete
22+
}
23+
24+
public var state: State = .initializing {
25+
willSet {
26+
print("new state=\(String(describing: newValue))")
27+
}
28+
29+
didSet {
30+
guard oldValue != state else { return }
31+
print("old state=\(String(describing: oldValue))")
32+
}
33+
}
34+
}
35+
36+
37+
let m = Model()
38+
39+
// CHECK: new state=running
40+
// CHECK: old state=initializing
41+
m.state = .running
42+
// CHECK: new state=complete
43+
// CHECK: old state=running
44+
m.state = .complete

0 commit comments

Comments
 (0)