Stream: beginners

Topic: Decoding List Bools


view this post on Zulip Luke Boswell (Mar 17 2023 at 07:14):

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.

view this post on Zulip Luke Boswell (Mar 17 2023 at 07:15):

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

view this post on Zulip Brendan Hansknecht (Mar 17 2023 at 07:24):

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.

view this post on Zulip Brendan Hansknecht (Mar 17 2023 at 07:25):

That is at least my understanding.

view this post on Zulip Luke Boswell (Mar 17 2023 at 07:27):

Both Bool and List have DecoderFormatting

bool : Decoder Bool fmt | fmt has DecoderFormatting
list : Decoder elem fmt -> Decoder (List elem) fmt | fmt has DecoderFormatting

view this post on Zulip Luke Boswell (Mar 17 2023 at 07:28):

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.

view this post on Zulip Brendan Hansknecht (Mar 17 2023 at 14:06):

What is the difference between decoder and decoding abilities?

view this post on Zulip Brendan Hansknecht (Mar 17 2023 at 14:07):

Oh, also, i think i have a guess as to what is happening. I'll take a look when I get the chance

view this post on Zulip Luke Boswell (Mar 19 2023 at 10:02):

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:

view this post on Zulip Brendan Hansknecht (Mar 20 2023 at 03:09):

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.

view this post on Zulip Brendan Hansknecht (Mar 20 2023 at 03:10):

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?

view this post on Zulip Brendan Hansknecht (Mar 20 2023 at 03:14):

Oh, actually, now that I think about it, Bool is an opaque type defined in Roc.

view this post on Zulip Brendan Hansknecht (Mar 20 2023 at 03:14):

I think we just need to add the definitions here.

view this post on Zulip Brendan Hansknecht (Mar 20 2023 at 03:16):

Bool should have the Decoding and Encoding abilities. It's methods should just call directly into the bool method.

view this post on Zulip Brendan Hansknecht (Mar 20 2023 at 03:21):

Something like:

Bool := [True, False] has [Eq { isEq: boolIsEq }, Decoding { decoder: boolDecoder }]

boolDecoder = \bytes, fmt -> Decode.bool bytes fmt

view this post on Zulip Brendan Hansknecht (Mar 20 2023 at 03:21):

Then need to add in any need imports and pipe it through like other builtin functions.

view this post on Zulip Brendan Hansknecht (Mar 20 2023 at 03:21):

Of course also adding it for Encoding.

view this post on Zulip Brendan Hansknecht (Mar 20 2023 at 03:26):

Hmm...though this has a chicken and egg problem. Decode imports Bool and Bool imports Decode.....

view this post on Zulip Brendan Hansknecht (Mar 20 2023 at 03:27):

This is why Eq had to be defined in Bool.roc. That doesn't seem reasonable for Decode though.

view this post on Zulip Ayaz Hafiz (Mar 20 2023 at 14:17):

ill take care of this today

view this post on Zulip Ayaz Hafiz (Mar 20 2023 at 20:06):

https://github.com/roc-lang/roc/pull/5166 should take care of this

view this post on Zulip Luke Boswell (Mar 20 2023 at 20:48):

Thank you Ayaz :big_smile:


Last updated: Jul 05 2025 at 12:14 UTC