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
I think that's still the general API we want. One second
Previous discussion here
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
decoding a tag decodes the discriminant and then the correct sequence
How would the sequence section of this API know about the tag decoded in the discriminant step?
Sorry, questioned it without taking the time to try it out. I understand it now
Last updated: Jul 06 2025 at 12:14 UTC