Stream: beginners

Topic: How to decode json tag union


view this post on Zulip Erik Karlsson (Dec 05 2023 at 10:43):

Hi,

New to Roc, I'm trying to decode one of two possible json values, but I'm running in to this problem:

thread '<unnamed>' panicked at 'Ability specialization is unknown - code generation cannot proceed!: DeriveError(Underivable)', crates/compiler/mono/src/ir.rs:6146:10
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

The interesting part of the code is this:

    JsonDecoded : List [Empty Str, Occupied { occupied : Str }]
    decoded : DecodeResult JsonDecoded
    decoded =
        "[\"empty\", {\"occupied\": \"user\"}]"
        |> Str.toUtf8
        |> fromBytesPartial (jsonWithOptions { fieldNameMapping: KebabCase })

I'm using lukewilliamboswell/roc-json 0.4.0, the full code can be found here https://gist.github.com/souze/5cbb51e1df1563bd588e5b3254ade59b

I've also tried with
JsonDecoded : List [Empty, Occupied Str]
and
JsonDecoded : List [Empty, Occupied {occupied: Str}]

But with the same result.

view this post on Zulip Luke Boswell (Dec 05 2023 at 12:12):

Decoding tags is currently not implemented in the compiler. You can see the implementation of the Decode ability in crates/compiler/builtins/roc/Decode.roc. That will most likely be what is causing this issue.

view this post on Zulip Luke Boswell (Dec 05 2023 at 12:14):

I think @Isaac Van Doren is currently working on implementing these, here is an ongoing discussion on the topic

view this post on Zulip Erik Karlsson (Dec 05 2023 at 13:06):

I see, thanks a lot! I'll keep an eye out, in the meantime, maybe I can hand-parse this specific case :)


Last updated: Jul 05 2025 at 12:14 UTC