Stream: beginners

Topic: Trying to add decodeDict


view this post on Zulip Seth Workman (Mar 16 2023 at 02:48):

I'm trying to add an ability for dict in Decode.roc and the relevant function in Json.roc. When doing roc check on Json.roc I'm getting the error?

The DecoderFormatting ability does not have a member dict

120│               dict: decodeDict,
                   ^^^^

Only implementations for members an ability has can be specified in
this location.

view this post on Zulip Brendan Hansknecht (Mar 16 2023 at 02:59):

Can you share the larger chunk of surrounding code?

view this post on Zulip Seth Workman (Mar 16 2023 at 03:07):

Sure, here is relevant part of Json.roc with the issue. You can see I added the member dict at the bottom

Json := {} has [
         EncoderFormatting {
             u8: encodeU8,
             u16: encodeU16,
             u32: encodeU32,
             u64: encodeU64,
             u128: encodeU128,
             i8: encodeI8,
             i16: encodeI16,
             i32: encodeI32,
             i64: encodeI64,
             i128: encodeI128,
             f32: encodeF32,
             f64: encodeF64,
             dec: encodeDec,
             bool: encodeBool,
             string: encodeString,
             list: encodeList,
             record: encodeRecord,
             tag: encodeTag,
         },
         DecoderFormatting {
             u8: decodeU8,
             u16: decodeU16,
             u32: decodeU32,
             u64: decodeU64,
             u128: decodeU128,
             i8: decodeI8,
             i16: decodeI16,
             i32: decodeI32,
             i64: decodeI64,
             i128: decodeI128,
             f32: decodeF32,
             f64: decodeF64,
             dec: decodeDec,
             bool: decodeBool,
             string: decodeString,
             list: decodeList,
             record: decodeRecord,
             dict: decodeDict,
         },
     ]

and I added to Decode.roc and also exposed it

DecoderFormatting has
    u8 : Decoder U8 fmt | fmt has DecoderFormatting
    u16 : Decoder U16 fmt | fmt has DecoderFormatting
    u32 : Decoder U32 fmt | fmt has DecoderFormatting
    u64 : Decoder U64 fmt | fmt has DecoderFormatting
    u128 : Decoder U128 fmt | fmt has DecoderFormatting
    i8 : Decoder I8 fmt | fmt has DecoderFormatting
    i16 : Decoder I16 fmt | fmt has DecoderFormatting
    i32 : Decoder I32 fmt | fmt has DecoderFormatting
    i64 : Decoder I64 fmt | fmt has DecoderFormatting
    i128 : Decoder I128 fmt | fmt has DecoderFormatting
    f32 : Decoder F32 fmt | fmt has DecoderFormatting
    f64 : Decoder F64 fmt | fmt has DecoderFormatting
    dec : Decoder Dec fmt | fmt has DecoderFormatting
    bool : Decoder Bool fmt | fmt has DecoderFormatting
    string : Decoder Str fmt | fmt has DecoderFormatting
    list : Decoder elem fmt -> Decoder (List elem) fmt | fmt has DecoderFormatting
    record : state, (state, Str -> [Keep (Decoder state fmt), Skip]), (state -> Result val DecodeError) -> Decoder val fmt | fmt has DecoderFormatting
    dict : Decoder elem fmt -> Decoder (Dict Str elem) fmt | fmt has DecoderFormatting

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

Ah, yeah, you can't add a new value there.

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

DecodeFormatting is defined here: https://github.com/roc-lang/roc/blob/a80b25d0447666719d26eb5717c62c7fe6b9a54f/crates/compiler/builtins/roc/Decode.roc#L62

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

All other types have to be defined in terms of those primatives.

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

I believe you would need to define the Decoding ability for Dict or some Dict wrapper type (though should get added to the standard library Dict: https://github.com/roc-lang/roc/blob/a80b25d0447666719d26eb5717c62c7fe6b9a54f/crates/compiler/builtins/roc/Decode.roc#L59

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

Decoding uses those primitives to decode a list of bytes into another type. in this case, Dict.

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

So you probably want to use create a definition that first decodes a List (k, v) Then it creates a dictionary from the list.

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

Or something of that nature.

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

Though I guess json has a native dict type, so maybe we need native decoding for it, I am not fully sure how these things compose.

view this post on Zulip Seth Workman (Mar 16 2023 at 15:29):

Turns out my question was with the fact I needed to run cargo build in order to get the new DecoderFormatting member dict

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

@Seth Workman I'm interested to know how you went here? Any success adding Dicts for Json decoding?

view this post on Zulip Seth Workman (Mar 20 2023 at 03:24):

Not far enough, just got a generic method compiling but nothing actually working. I don't have enough knowledge of how its all linked together yet


Last updated: Jul 06 2025 at 12:14 UTC