forked from swiftlang/swift
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResult.swift
150 lines (124 loc) · 3.41 KB
/
Result.swift
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
141
142
143
144
145
146
147
148
149
150
//===--- Result.swift -----------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// RUN: %target-run-stdlib-swift
// REQUIRES: executable_test
publicenumResult<Value>{
case Success(Value)
case Error(Error)
init(success x:Value){
self=.Success(x)
}
init(error:Error){
self=.Error(error)
}
func map<U>(_ transform:(Value)->U)->Result<U>{
switchself{
case.Success(let x):return.Success(transform(x))
case.Error(let e):return.Error(e)
}
}
func flatMap<U>(_ transform:(Value)->Result<U>)->Result<U>{
switchself{
case.Success(let x):returntransform(x)
case.Error(let e):return.Error(e)
}
}
func get()throws->Value{
switchself{
case.Success(let x):return x
case.Error(let e):throw e
}
}
varsuccess:Value?{
switchself{
case.Success(let x):return x
case.Error:returnnil
}
}
varerror:Error?{
switchself{
case.Success:returnnil
case.Error(let x):return x
}
}
}
publicfunc??<T>(
result:Result<T>, defaultValue:@autoclosure()->T
)->T{
switch result {
case.Success(let x):return x
case.Error:returndefaultValue()
}
}
// We aren't actually proposing this overload; we think there should
// be a compiler warning that catches the promotion that you probably
// don't want.
publicfunc??<T>(
result:Result<T>?, defaultValue:@autoclosure()->T
)->T{
fatalError("We should warn about Result<T> being promoted to Result<T>?")
}
/// Translate the execution of a throwing closure into a Result
func catchResult<Success>(
invoking body:()throws->Success
)->Result<Success>{
do{
returntry.Success(body())
}
catch{
return.Error(error)
}
}
// A couple of error types
enumNasty:Error{
case Bad, Awful, Terrible
}
enumIcky:Error{
case Sad, Bad, Poor
}
// Some Results to work with
letthree=Result(success:3)
letfour=Result(success:4)
letnasty=Result<Int>(error:Nasty.Bad)
leticky=Result<String>(error:Icky.Sad)
print(three)
print(nasty)
print(icky)
print(three ??4)
print(nasty ??4)
print(three.map{String($0)})
print(nasty.map{String($0)})
print(three.flatMap{.Success(String($0))})
print(nasty.flatMap{.Success(String($0))})
print(three.flatMap{ _ in icky })
print(nasty.flatMap{ _ in icky })
tryprint(three.get())
do{
tryprint(nasty.get())
}
catch{
print(error)
}
func mayFail(_ fail:Bool)throws->Int{
if fail {throwIcky.Poor }
return0
}
print(catchResult{trymayFail(true)})
print(catchResult{trymayFail(false)})
print(catchResult{1}.flatMap{ _ inResult(success:4)}.flatMap{ _ inResult<String>(error:Icky.Poor)})
print(catchResult{1}.map{ _ in three }.flatMap{$0})
letresults=[three, nasty, four]
print(results.flatMap{ $0.success })
print(results.flatMap{ $0.error })
print(results.contains{ $0.success !=nil})
// Mistaken usage; causes Result<T> to be promoted to Result<T>?
// print(three ?? nasty)