Is it possible to implement Decoding
for a List Bool
? I've been scratching my head on this one for a little while now...
── TYPE MISMATCH ──────────────────────────────────────────────────── Json.roc ─
This expression has a type that does not implement the abilities it's expected to:
536│ actual = Decode.fromBytes input fromUtf8
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
I can't generate an implementation of the Decoding ability for
List Bool
In particular, an implementation for
Bool
cannot be generated.
Tip: Bool does not implement Decoding.
This is the test I am looking at,
# Test decode bool
expect
input = Str.toUtf8 "[ true,false ,true, false ]"
actual : Result (List Bool) _
actual = Decode.fromBytes input fromUtf8
expected = Ok [Bool.true, Bool.false, Bool.true, Bool.false]
actual == expected
Hmm. Since it is an opaque type, i would guess the definition would need to be directly in the bool module. Since that wouldn't make sense for every encoding and decoding format, i think bool would need to be added as a primitive to the decodeformating ability.
That is at least my understanding.
Both Bool
and List
have DecoderFormatting
bool : Decoder Bool fmt | fmt has DecoderFormatting
list : Decoder elem fmt -> Decoder (List elem) fmt | fmt has DecoderFormatting
Yeah I think there is something up with Bool
── TYPE MISMATCH ─────────────────────── crates/compiler/builtins/roc/Json.roc ─
This expression has a type that does not implement the abilities it's expected to:
536│ actual = Decode.fromBytes input fromUtf8
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The type Bool does not fully implement the ability Decoding.
What is the difference between decoder and decoding abilities?
Oh, also, i think i have a guess as to what is happening. I'll take a look when I get the chance
I'm still stumped on this issue. I haven't been able to figure out why Bool
doesn't fully implement Decoding
. :smiling_face_with_tear:
Oh, I meant to look into this early. I think the reason Bool
doesn't work is that we never added the code to the roc compiler to auto derive Decode/Hash/etc
for Bool
.
I'm not sure I know exactly how to add it, but I think that is fundamentally the issue. Maybe @Ayaz Hafiz has some insights?
Oh, actually, now that I think about it, Bool
is an opaque type defined in Roc.
I think we just need to add the definitions here.
Bool
should have the Decoding
and Encoding
abilities. It's methods should just call directly into the bool method.
Something like:
Bool := [True, False] has [Eq { isEq: boolIsEq }, Decoding { decoder: boolDecoder }]
boolDecoder = \bytes, fmt -> Decode.bool bytes fmt
Then need to add in any need imports and pipe it through like other builtin functions.
Of course also adding it for Encoding
.
Hmm...though this has a chicken and egg problem. Decode
imports Bool
and Bool
imports Decode
.....
This is why Eq
had to be defined in Bool.roc
. That doesn't seem reasonable for Decode
though.
ill take care of this today
https://github.com/roc-lang/roc/pull/5166 should take care of this
Thank you Ayaz :big_smile:
Last updated: Jul 05 2025 at 12:14 UTC