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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
// RUN: %target-run-simple-swift(-parse-as-library -Xfrontend -disable-availability-checking) | %FileCheck %s
// REQUIRES: executable_test
// REQUIRES: concurrency
// UNSUPPORTED: freestanding
// rdar://76038845
// REQUIRES: concurrency_runtime
// UNSUPPORTED: back_deployment_runtime
enum GeneralError : Error {
case UnknownBallKind
case Todo
}
enum BallKind {
case MostLostV1
case Chromehard
case PT5
case KirksandLignature
}
@available(SwiftStdlib 5.1, *)
class Specs {
// obtains the number of dimples
subscript(_ bk : BallKind) -> Int {
get throws {
switch (bk) {
case .MostLostV1:
return 450
case .Chromehard:
return 125
default:
throw GeneralError.UnknownBallKind
}
}
}
}
@available(SwiftStdlib 5.1, *)
actor Database {
var currentData : Specs {
get async {
let handle = detach { Specs() }
print("obtaining specs...")
return await handle.get()
}
}
var hasNewData : Bool {
get throws { return true }
}
}
@available(SwiftStdlib 5.1, *)
protocol SphericalObject {
var name : String { get async throws }
var dimples : Int { get async throws }
var description : String { get async throws }
}
@available(SwiftStdlib 5.1, *)
class Ball : SphericalObject {
var name : String { get async throws { throw GeneralError.Todo } }
var dimples : Int { get async throws { throw GeneralError.Todo } }
var description : String { get async throws { throw GeneralError.Todo } }
}
@available(SwiftStdlib 5.1, *)
class GolfBall : Ball {
private static let db : Database = Database()
private var _model : BallKind
private var _dimples : Int?
init(_ bk : BallKind) {
_model = bk
}
override var name : String {
return "golf ball"
}
override var description : String {
get async throws {
return "this \(name) has \(await dimples) dimples"
}
}
override var dimples : Int {
get async {
let newData = (try? await GolfBall.db.hasNewData) ?? false
if newData || _dimples == nil {
let specs = await GolfBall.db.currentData
_dimples = (try? specs[_model]) ?? 0
}
return _dimples!
}
}
}
// CHECK: obtaining specs...
// CHECK: this golf ball has 450 dimples
// CHECK: obtaining specs...
// CHECK: this golf ball has 125 dimples
// CHECK: obtaining specs...
// CHECK: this golf ball has 0 dimples
// CHECK: obtaining specs...
// CHECK: this golf ball has 0 dimples
@available(SwiftStdlib 5.1, *)
func printAsBall(_ b : Ball) async {
print(try! await b.description)
}
@available(SwiftStdlib 5.1, *)
func printAsAsSphericalObject(_ b : SphericalObject) async {
print(try! await b.description)
}
@available(SwiftStdlib 5.1, *)
@main struct RunIt {
static func main() async {
let balls : [(Bool, Ball)] = [
(true, GolfBall(.MostLostV1)),
(false, GolfBall(.Chromehard)),
(true, GolfBall(.PT5)),
(false, GolfBall(.KirksandLignature))
]
for (useProtocol, ball) in balls {
if (useProtocol) {
await printAsAsSphericalObject(ball)
} else {
await printAsBall(ball)
}
}
}
}
|