Stream: compiler development

Topic: Decode ability for tagged union


view this post on Zulip Nathan Kamenchu (Dec 25 2024 at 23:30):

I'm assuming it's not possible to handle implement Decode for tagged unions without some form of a macro system. Have there been any previous discussions of this? It does feel a bit incomplete for this to be missing

view this post on Zulip Richard Feldman (Dec 25 2024 at 23:32):

it's possible, we just haven't implemented it

view this post on Zulip Richard Feldman (Dec 25 2024 at 23:32):

it shouldn't need a macro system

view this post on Zulip Brendan Hansknecht (Dec 25 2024 at 23:32):

We haven't implemented it? I thought it already worked

view this post on Zulip Brendan Hansknecht (Dec 25 2024 at 23:32):

Just isn't implemented for the JSON decoder

view this post on Zulip Richard Feldman (Dec 25 2024 at 23:33):

I don't think we have it for Decoding

view this post on Zulip Richard Feldman (Dec 25 2024 at 23:33):

but I could be misremembering :big_smile:

view this post on Zulip Brendan Hansknecht (Dec 25 2024 at 23:33):

Oh... intriguing. I don't recall at this point. That said, we still plan to revamp the whole system.

view this post on Zulip Brendan Hansknecht (Dec 25 2024 at 23:34):

But yeah, no macro system needed.

view this post on Zulip Brendan Hansknecht (Dec 25 2024 at 23:35):

You have autoderive or a manual implementation. Either is fine.

view this post on Zulip Nathan Kamenchu (Dec 25 2024 at 23:36):

It's also missing from the Ability definition

view this post on Zulip Brendan Hansknecht (Dec 25 2024 at 23:38):

Ok

view this post on Zulip Brendan Hansknecht (Dec 25 2024 at 23:39):

Last I checked with the work to revamp decode, it was stuck on lambdaset bugs.

view this post on Zulip Brendan Hansknecht (Dec 25 2024 at 23:39):

But we should be able to add tag decoding directly to the old decode

view this post on Zulip Nathan Kamenchu (Dec 25 2024 at 23:43):

The compiler panics with

thread '<unnamed>' panicked at crates/compiler/mono/src/ir.rs:6257:10:
Ability specialization is unknown. Tip: check out <https://roc.zulipchat.com/#na
rrow/stream/231634-beginners/topic/Non-Functions.20in.20Abilities/near/456068617
>: DeriveError(Underivable)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

view this post on Zulip Nathan Kamenchu (Dec 25 2024 at 23:49):

I was trying out roc and decided to implement lamdera in roc by writing a platform but I've gotten stuck on sending messages over the wire.

view this post on Zulip Brendan Hansknecht (Dec 25 2024 at 23:50):

This is with a tag?

view this post on Zulip Nathan Kamenchu (Dec 25 2024 at 23:50):

Yes that is what happens with a tag
There's a different but similar message for Opaque types too

view this post on Zulip Brendan Hansknecht (Dec 25 2024 at 23:51):

If so, we need to add tag decoding to the ability. Then we need to add autoderive for it in the compiler.

view this post on Zulip Brendan Hansknecht (Dec 25 2024 at 23:51):

Would be very similar to the autoderrive for encoding a tag

view this post on Zulip Nathan Kamenchu (Dec 25 2024 at 23:56):

Trying to decode with an opaque type errors with

An internal compiler expectation was broken.

This is definitely a compiler bug.

Please file an issue here: <https://github.com/roc-lang/roc/issues/new/choose>

expected to know a specialization for `17.IdentId(2)`#`13.IdentId(4)`, but it wasn't found

Location: crates/compiler/solve/src/specialize.rs:749:29

But my assumption is that this is intentional

view this post on Zulip Nathan Kamenchu (Dec 26 2024 at 00:00):

Brendan Hansknecht said:

Would be very similar to the autoderrive for encoding a tag

Should I create an issue for it? You mentioned that there is work to revamp the code
I will take a look at the compiler and try to implement an autoderive.

view this post on Zulip Nathan Kamenchu (Dec 27 2024 at 12:44):

What is reasonable API for a tag method in the DeocderFormatting ability?

view this post on Zulip Brendan Hansknecht (Dec 27 2024 at 16:22):

With the current decode it should be very similar to a tuple. It should just decode a string tag name before decoding the tuple fields

view this post on Zulip Nathan Kamenchu (Dec 27 2024 at 16:32):

I assume something similar to record and tuple like
tuple : state, (state, Str -> [Select (Decoder state fmt), Next]), (state -> Result val DecodeError) -> Decoder val fmt where fmt implements DecoderFormatting
but the nested decoding requirements where each variant would then need to decode a variable number of fields does feel like it may require more information like the index of the current tuple field

view this post on Zulip Nathan Kamenchu (Dec 27 2024 at 16:43):

The alternative would just be push all of that to the state


Last updated: Jul 06 2025 at 12:14 UTC