Stream: API design

Topic: tagged union decoders API design


view this post on Zulip Nathan Kamenchu (Dec 27 2024 at 17:35):

This is continuation of the thread #compiler development > Decode ability for tagged union
I've moved it here for input on the API design

I've seen discussions of this in the issue https://github.com/roc-lang/roc/issues/3816 though this is from 2022

view this post on Zulip Ayaz Hafiz (Dec 27 2024 at 17:45):

I think that's still the general API we want. One second

view this post on Zulip Ayaz Hafiz (Dec 27 2024 at 17:47):

Previous discussion here

view this post on Zulip Ayaz Hafiz (Dec 27 2024 at 17:47):

#ideas > encoding tag union indices @ 💬

view this post on Zulip Ayaz Hafiz (Dec 27 2024 at 17:47):

When we first implemented decoding/encoding this is what I suggest for the decoding API

## `discriminant {tagNames, maxIndex}` decodes the index of a tag given the names of the tags and the number of tags in the definition.
discriminant : {tagNames: List (List U8), maxIndex: U64} -> Decoder U64 fmt | fmt has DecoderFormatting

## `sequence state stepElem finalizer` decodes a possibly-heterogenous sequence representation into `state`.
sequence : state, (state -> [Keep (Decoder state fmt), Skip]), (state -> Result val DecodeError) -> Decoder val fmt | fmt has DecoderFormatting

view this post on Zulip Ayaz Hafiz (Dec 27 2024 at 17:48):

decoding a tag decodes the discriminant and then the correct sequence

view this post on Zulip Nathan Kamenchu (Dec 27 2024 at 21:24):

How would the sequence section of this API know about the tag decoded in the discriminant step?

view this post on Zulip Nathan Kamenchu (Dec 27 2024 at 21:47):

Sorry, questioned it without taking the time to try it out. I understand it now


Last updated: Jul 06 2025 at 12:14 UTC