Skip to content

Commit b3676b9

Browse files
palascarbolymer
andcommitted
Remove unnecessary usage of GADTs
Co-authored-by: Mateusz Galazyn <[email protected]>
1 parent 5582a19 commit b3676b9

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

docs/ADR-10-Callstacks-for-API-errors.md

+8-11
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,14 @@ class ErrorContent e where
5757
And then, we could define our extended `Error` as:
5858

5959
```haskell
60-
data Content where
61-
Content :: ErrorContent e => e -> Content
60+
data Content = forall e. ErrorContent e => Content e
6261

6362
class Error e where
6463
getErrorContent :: e -> Content
6564
getErrorCallStack :: e -> CallStack
6665
```
6766

68-
We need a `Content` wrapper as a `GADT`, to prevent the type of the `ErrorContent` from propagating to the type of `Error`, while ensuring that it is indeed an `ErrorContent`.
69-
67+
We need `e` in `Content` wrapper to be an existential type, to prevent the type of the `ErrorContent` from propagating to the type of `Error`, while ensuring that it is indeed an `ErrorContent`.
7068
If we do this, we could define two generic functions for every `Error e`.
7169

7270
One to print the original error, without a stack-trace:
@@ -137,8 +135,7 @@ class Error e where
137135
Where `Cause` is another wrapper like `Content`:
138136

139137
```haskell
140-
data Cause where
141-
Cause :: Error c => c -> Cause
138+
data Cause = forall c. Error c => Cause c
142139
```
143140

144141
And that would allow us to define `prettyError` as follows:
@@ -172,15 +169,15 @@ But, other than that, it comes down to adding a space for the stack-trace in the
172169

173170
At least, we can have a reusable wrapper for errors (that must now implement the `ErrorContent` class).
174171

175-
So we can define an `ErrorWithStack` data type using a `GADT` as follows:
172+
So we can define an `ErrorWithStack` data type:
176173

177174
```haskell
178-
data ErrorWithStack e where
179-
RootErrorWithStack :: ErrorContent e => e -> CallStack -> ErrorWithStack e
180-
CausedErrorWithStack :: (ErrorContent e, Error c) => e -> CallStack -> c -> ErrorWithStack e
175+
data ErrorWithStack e =
176+
ErrorContent e => RootErrorWithStack e CallStack
177+
| forall c. (ErrorContent e, Error c) => CausedErrorWithStack e CallStack c
181178
```
182179

183-
The difference between the constructors is that one has a causing error, and the otherone doesn't.
180+
The difference between the constructors is that one has a causing error, and the other one doesn't.
184181

185182
We can then create convenience functions for creating them from existing `ErrorContent`s:
186183

0 commit comments

Comments
 (0)