Skip to content

Commit 7ef601e

Browse files
committed
Unify error types
1 parent dcf7b56 commit 7ef601e

File tree

6 files changed

+18
-33
lines changed

6 files changed

+18
-33
lines changed

gen/src/[email protected]

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ contains(List, Elem) ->
2424
head(List) ->
2525
case List of
2626
[] ->
27-
{error, empty};
27+
{error, {}};
2828

2929
[X | _] ->
3030
{ok, X}
@@ -33,7 +33,7 @@ head(List) ->
3333
tail(List) ->
3434
case List of
3535
[] ->
36-
{error, empty};
36+
{error, {}};
3737

3838
[_ | Xs] ->
3939
{ok, Xs}
@@ -172,7 +172,7 @@ fold_right(List, Acc, Fun) ->
172172
find(Haystack, F) ->
173173
case Haystack of
174174
[] ->
175-
{error, not_found};
175+
{error, {}};
176176

177177
[X | Rest] ->
178178
case F(X) of
@@ -232,7 +232,7 @@ strict_zip(L1, L2) ->
232232
{ok, zip(L1, L2)};
233233

234234
false ->
235-
{error, length_mismatch}
235+
{error, {}}
236236
end.
237237

238238
intersperse(List, Elem) ->
@@ -250,12 +250,12 @@ intersperse(List, Elem) ->
250250
at(List, I) ->
251251
case I < 0 of
252252
true ->
253-
{error, not_found};
253+
{error, {}};
254254

255255
false ->
256256
case List of
257257
[] ->
258-
{error, not_found};
258+
{error, {}};
259259

260260
[X | Rest] ->
261261
case I =:= 0 of

gen/src/[email protected]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ update(Dict, Key, F) ->
7272
put(Dict, Key, F({ok, Value}));
7373

7474
{error, _} ->
75-
put(Dict, Key, F({error, not_found}))
75+
put(Dict, Key, F({error, nil}))
7676
end.
7777

7878
do_fold(List, Acc, F) ->

src/gleam/int.gleam

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import gleam/order
22

3-
pub enum NotAnInt =
4-
| NotAnInt
5-
6-
pub external fn parse(String) -> Result(Int, NotAnInt) = "gleam__stdlib" "parse_int";
3+
pub external fn parse(String) -> Result(Int, Nil) = "gleam__stdlib" "parse_int";
74

85
pub external fn to_string(Int) -> String = "erlang" "integer_to_binary"
96

src/gleam/list.gleam

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,7 @@ import gleam/int
22
import gleam/order
33
import gleam/pair
44

5-
pub enum Empty =
6-
| Empty
7-
8-
pub enum NotFound =
9-
| NotFound
10-
11-
pub enum LengthMismatch =
12-
| LengthMismatch
5+
pub struct LengthMismatch {}
136

147
// Using the Erlang C BIF implementation.
158
//
@@ -32,14 +25,14 @@ pub fn contains(list, elem) {
3225

3326
pub fn head(list) {
3427
case list {
35-
| [] -> Error(Empty)
28+
| [] -> Error(Nil)
3629
| [x | _] -> Ok(x)
3730
}
3831
}
3932

4033
pub fn tail(list) {
4134
case list {
42-
| [] -> Error(Empty)
35+
| [] -> Error(Nil)
4336
| [_ | xs] -> Ok(xs)
4437
}
4538
}
@@ -157,7 +150,7 @@ pub fn fold_right(list, acc, fun) {
157150

158151
pub fn find(haystack, f) {
159152
case haystack {
160-
| [] -> Error(NotFound)
153+
| [] -> Error(Nil)
161154
| [x | rest] ->
162155
case f(x) {
163156
| Ok(x) -> Ok(x)
@@ -213,10 +206,10 @@ pub fn intersperse(list, elem) {
213206

214207
pub fn at(list, i) {
215208
case i < 0 {
216-
| True -> Error(NotFound)
209+
| True -> Error(Nil)
217210
| False ->
218211
case list {
219-
| [] -> Error(NotFound)
212+
| [] -> Error(Nil)
220213
| [x | rest] ->
221214
case i == 0 {
222215
| True -> Ok(x)

src/gleam/map.gleam

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ import gleam/pair
55

66
pub external type MapDict(key, value);
77

8-
pub enum NotFound =
9-
| NotFound
10-
118
pub external fn size(MapDict(k, v)) -> Int
129
= "maps" "size"
1310

@@ -27,7 +24,7 @@ pub fn has_key(map, key) {
2724
pub external fn new() -> MapDict(key, value)
2825
= "maps" "new"
2926

30-
pub external fn fetch(MapDict(key, value), key) -> Result(value, NotFound)
27+
pub external fn fetch(MapDict(key, value), key) -> Result(value, Nil)
3128
= "gleam__stdlib" "map_fetch";
3229

3330
external fn erl_put(key, value, MapDict(key, value)) -> MapDict(key, value)
@@ -79,12 +76,10 @@ pub fn drop(map, keys) {
7976
})
8077
}
8178

82-
pub external type NotFound;
83-
8479
pub fn update(dict, key, f) {
8580
case fetch(dict, key) {
8681
| Ok(value) -> put(dict, key, f(Ok(value)))
87-
| Error(_) -> put(dict, key, f(Error(NotFound)))
82+
| Error(_) -> put(dict, key, f(Error(Nil)))
8883
}
8984
}
9085

src/gleam__stdlib.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ expect_is_error(A) -> ?assertMatch({error, _}, A).
1717

1818
map_fetch(Map, Key) ->
1919
case maps:find(Key, Map) of
20-
error -> {error, not_found};
20+
error -> {error, nil};
2121
OkFound -> OkFound
2222
end.
2323

@@ -79,7 +79,7 @@ parse_int(String) ->
7979
{ok, Integer};
8080

8181
_ ->
82-
{error, parse_error}
82+
{error, nil}
8383
end.
8484

8585
parse_float(String) ->

0 commit comments

Comments
 (0)