forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathResult.swift
148 lines (122 loc) · 3.43 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
//===--- Result.swift -----------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// RUN: %target-run-stdlib-swift
// REQUIRES: executable_test
public enum Result<Value> {
case Success(Value)
case Error(ErrorType)
init(success x: Value) {
self = Success(x)
}
init(error: ErrorType) {
self = Error(error)
}
func map<U>(@noescape transform: (Value) -> U) -> Result<U> {
switch self {
case Success(let x): return .Success(transform(x))
case Error(let e): return .Error(e)
}
}
func flatMap<U>(@noescape transform: (Value) -> Result<U>) -> Result<U> {
switch self {
case Success(let x): return transform(x)
case Error(let e): return .Error(e)
}
}
func get() throws -> Value {
switch self {
case Success(let x): return x
case Error(let e): throw e
}
}
var success: Value? {
switch self {
case Success(let x): return x
case Error: return nil
}
}
var error: ErrorType? {
switch self {
case Success: return nil
case Error(let x): return x
}
}
}
public func ?? <T> (
result: Result<T>, @autoclosure defaultValue: () -> T
) -> T {
switch result {
case .Success(let x): return x
case .Error: return defaultValue()
}
}
// 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.
public func ?? <T> (
result: Result<T>?, @autoclosure defaultValue: () -> 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>(body: () throws -> Success) -> Result<Success> {
do {
return try .Success(body())
}
catch {
return .Error(error)
}
}
// A couple of error types
enum Nasty : ErrorType {
case Bad, Awful, Terrible
}
enum Icky : ErrorType {
case Sad, Bad, Poor
}
// Some Results to work with
let three = Result(success: 3)
let four = Result(success: 4)
let nasty = Result<Int>(error: Nasty.Bad)
let icky = 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 })
try print(three.get())
do {
try print(nasty.get())
}
catch {
print(error)
}
func mayFail(fail: Bool) throws -> Int {
if fail { throw Icky.Poor }
return 0
}
print(catchResult { try mayFail(true) })
print(catchResult { try mayFail(false) })
print(catchResult { _ in 1 }.flatMap { _ in Result(success: 4) }.flatMap { _ in Result<String>(error: Icky.Poor) })
print(catchResult { _ in 1 }.map { _ in three }.flatMap {$0} )
let results = [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)