-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAttempt.php
177 lines (161 loc) · 3.38 KB
/
Attempt.php
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
declare(strict_types = 1);
namespace Innmind\Immutable;
use Innmind\Immutable\Attempt\{
Implementation,
Error,
Result,
Defer,
};
/**
* @template-covariant T
* @psalm-immutable
*/
final class Attempt
{
/** @var Implementation<T> */
private Implementation $implementation;
/**
* @param Implementation<T> $implementation
*/
private function __construct(Implementation $implementation)
{
$this->implementation = $implementation;
}
/**
* @template U
* @psalm-pure
*
* @return self<U>
*/
public static function error(\Throwable $error): self
{
return new self(new Error($error));
}
/**
* @template U
* @psalm-pure
*
* @param U $value
*
* @return self<U>
*/
public static function result(mixed $value): self
{
return new self(new Result($value));
}
/**
* This method is to be used for IO operations
*
* @template U
* @psalm-pure
*
* @param callable(): self<U> $deferred
*
* @return self<U>
*/
public static function defer(callable $deferred): self
{
return new self(new Defer($deferred));
}
/**
* @template U
* @psalm-pure
*
* @param callable(): U $try
*
* @return self<U>
*/
public static function of(callable $try): self
{
try {
/** @psalm-suppress ImpureFunctionCall */
return self::result($try());
} catch (\Throwable $e) {
return self::error($e);
}
}
/**
* @template U
*
* @param callable(T): U $map
*
* @return self<U>
*/
public function map(callable $map): self
{
return new self($this->implementation->map($map));
}
/**
* @template U
*
* @param callable(T): self<U> $map
*
* @return self<U>
*/
public function flatMap(callable $map): self
{
return $this->implementation->flatMap($map);
}
/**
* @template U
*
* @param callable(T): U $result
* @param callable(\Throwable): U $error
*
* @return U
*/
public function match(callable $result, callable $error)
{
return $this->implementation->match($result, $error);
}
/**
* Be aware that this call is not safe as it may throw an exception.
*
* @throws \Throwable
*
* @return T
*/
public function unwrap(): mixed
{
/** @var T */
return $this->match(
static fn(mixed $value): mixed => $value,
static fn($e) => throw $e,
);
}
/**
* @template U
*
* @param callable(\Throwable): self<U> $recover
*
* @return self<T|U>
*/
public function recover(callable $recover): self
{
return $this->implementation->recover($recover);
}
/**
* @return Maybe<T>
*/
public function maybe(): Maybe
{
return $this->implementation->maybe();
}
/**
* @return Either<\Throwable, T>
*/
public function either(): Either
{
return $this->implementation->either();
}
/**
* Force loading the value in memory (only useful for a deferred Attempt)
*
* @return self<T>
*/
public function memoize(): self
{
return $this->implementation->memoize();
}
}