bs-decode
bs-decode copied to clipboard
decoding-variants#complex-variants
Hi folks, tried to run this example complex-variants
module R =
Decode.ParseError.ResultOf({
type t = [ Decode.ParseError.base | `InvalidColor | `InvalidShape];
let handle = x => (x :> t);
});
module D = Decode.Base.Make(R.TransformError, R);
type color = Blue | Red | Green;
let parseColor =
fun
| "Blue" => Ok(Blue)
| "Red" => Ok(Red)
| "Green" => Ok(Green)
| str => Error(Decode.ParseError.Val(`InvalidColor, Js.Json.string(str)));
let decodeColor = json => D.string |> Relude.Result.flatMap(parseColor);
Got the following error:
/DecodingVariantsComplex.re 18:39-71
16 │ | str => Error(Decode.ParseError.Val(`InvalidColor, Js.Json.string(s
tr)));
17 │
18 │ let decodeColor = json => D.string |> Relude.Result.flatMap(parseColor
);
This value might need to be wrapped in a function that takes an extra
parameter of type Js.Json.t
Here's the original error message
This has type:
Relude.Result.t(string, Decode.ParseError.t(([> `InvalidColor ] as 'a))) =>
Relude.Result.t(color, Decode.ParseError.t('a))
But somewhere wanted:
(Js.Json.t => R.t(Js.String.t)) => 'b
The incompatible parts:
Relude.Result.t(string, Decode.ParseError.t('a)) (defined as
Belt_Result.t(string, Decode.ParseError.t('a)))
vs
Js.Json.t => R.t(Js.String.t)
FAILED: subcommand failed.
>>>> Finish compiling(exit: 1)
Tried digging into the source but i am missing something. Thank you sir.
Oops, looks like a typo in the example... that json argument should probably be fed into the actual decoder. :) Try let decodeColor = json => D.string(json) |> Relude.Result.flatMap(parseColor); and if that works, I'll update the example.
Too bad we don't have typechecking for the examples to make sure this stuff works :P
Worked. Thank you, sir.
I hear it on the docs. A couple of spots need some linting. I will not be deterred!
Reopening, just to make sure I don't forget to go update those docs. :D