Skip to content

Commit 3d15571

Browse files
committed
Make decode.failure clearer
1 parent 7cde285 commit 3d15571

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## v0.67.0 - 2025-10-21
4+
5+
- The `failure` from the `dynamic/decode` module gains the `expected` label.
6+
- The `dynamic/decode` module now uses the term "placeholder value" rather than
7+
"zero value".
8+
39
## v0.66.0 - 2025-10-21
410

511
- The `tap` function from the `function` module has been deprecated.

src/gleam/dynamic/decode.gleam

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@
183183
//// "grass" -> decode.success(Grass)
184184
//// "electric" -> decode.success(Electric)
185185
//// // Return a failing decoder for any other strings
186-
//// _ -> decode.failure(Fire, "PocketMonsterType")
186+
//// _ -> decode.failure(Fire, expected: "PocketMonsterType")
187187
//// }
188188
//// }
189189
////
@@ -611,7 +611,9 @@ fn run_dynamic_function(
611611
) -> #(t, List(DecodeError)) {
612612
case f(data) {
613613
Ok(data) -> #(data, [])
614-
Error(zero) -> #(zero, [DecodeError(name, dynamic.classify(data), [])])
614+
Error(placeholder) -> #(placeholder, [
615+
DecodeError(name, dynamic.classify(data), []),
616+
])
615617
}
616618
}
617619

@@ -978,26 +980,37 @@ fn run_decoders(
978980
}
979981
}
980982

981-
/// Define a decoder that always fails. The parameter for this function is the
982-
/// name of the type that has failed to decode.
983+
/// Define a decoder that always fails.
983984
///
984-
/// When this decoder is used as part of a larger decoder, the zero value is
985-
/// used as a placeholder so that the rest of the decoder can continue to run
986-
/// and collect all decoding errors.
985+
/// The first parameter is a "placeholder" value, which is some default value that the
986+
/// decoder uses internally in place of the value that would have been produced
987+
/// if the decoder was successful. It doesn't matter what this value is, it is
988+
/// never returned by the decoder or shown to the user, so pick some arbitrary
989+
/// value. If it is an int you might pick `0`, if it is a list you might pick
990+
/// `[]`.
987991
///
988-
pub fn failure(zero: a, expected: String) -> Decoder(a) {
989-
Decoder(function: fn(d) { #(zero, decode_error(expected, d)) })
992+
/// The second parameter is the name of the type that has failed to decode.
993+
///
994+
/// ```gleam
995+
/// decode.failure("User", User(name: "", score: 0, tags: []))
996+
/// ```
997+
///
998+
pub fn failure(placeholder: a, expected name: String) -> Decoder(a) {
999+
Decoder(function: fn(d) { #(placeholder, decode_error(name, d)) })
9901000
}
9911001

9921002
/// Create a decoder for a new data type from a decoding function.
9931003
///
9941004
/// This function is used for new primitive types. For example, you might
9951005
/// define a decoder for Erlang's pid type.
9961006
///
997-
/// A default "zero" value is also required to make a decoder. When this
998-
/// decoder is used as part of a larger decoder this zero value is used as
999-
/// a placeholder so that the rest of the decoder can continue to run and
1000-
/// collect all decoding errors.
1007+
/// A default "placeholder" value is also required to make a decoder. When this
1008+
/// decoder is used as part of a larger decoder this placeholder value is used
1009+
/// so that the rest of the decoder can continue to run and
1010+
/// collect all decoding errors. It doesn't matter what this value is, it is
1011+
/// never returned by the decoder or shown to the user, so pick some arbitrary
1012+
/// value. If it is an int you might pick `0`, if it is a list you might pick
1013+
/// `[]`.
10011014
///
10021015
/// If you were to make a decoder for the `Int` type (rather than using the
10031016
/// build-in `Int` decoder) you would define it like so:
@@ -1030,7 +1043,9 @@ pub fn new_primitive_decoder(
10301043
Decoder(function: fn(d) {
10311044
case decoding_function(d) {
10321045
Ok(t) -> #(t, [])
1033-
Error(zero) -> #(zero, [DecodeError(name, dynamic.classify(d), [])])
1046+
Error(placeholder) -> #(placeholder, [
1047+
DecodeError(name, dynamic.classify(d), []),
1048+
])
10341049
}
10351050
})
10361051
}

0 commit comments

Comments
 (0)