0

I'm looking into protocol schema languages, and it seems like Cap'n'proto will suit my needs, but there's one critical feature I need which I cannot find in the docs:

Let's say I have this tagged union with two struct members in C-like syntax:

struct taggedUnion {
    int tag;
    union {
        struct a {
           int x;
        }

        struct b {
            float x;
        }
    }
}

Can I then in the future add another struct to the tagged union, while still being able to read the old data?

struct taggedUnion {
    int tag;
    union {
        struct a {
           int x;
        }

        struct b {
            float y;
        }

        struct c {
            int z;
            bool b;
        }
    }
}

It feels like it should be doable, but I can't find anything in the docs saying that it is. There's a note on groups being extensible without breaking wire-compatibility (new fields are zeroed out for old data).

If it's possible, how would I declare this change in cap'n proto schema syntax? A before/after example would be great!

Filip Haglund
  • 13,919
  • 13
  • 64
  • 113

1 Answers1

0

Found it. ... new fields may be added to existing groups and unions seems like it could be an answer to this question.

Filip Haglund
  • 13,919
  • 13
  • 64
  • 113
  • 1
    Yep, you can add members to unions. In fact, you can even create a new union wrapped around an existing field, and then add new fields to that union! This is called "retroactive unionization". – Kenton Varda Apr 11 '20 at 22:28
  • @KentonVarda It would be nice if it was mentioned explicitly on the website. I spent all morning reading docs and implementations (seems to be a limit of 2^16 union members as judged by reading the dlang code generator?) before I started looking for a way to contact you to get a proper response :) Thanks for clarifying! – Filip Haglund Apr 12 '20 at 10:11
  • 1
    Is is mentioned here (second bullet for regular unions, and seventh bullet for retroactive unionization): https://capnproto.org/language.html#evolving-your-protocol – Kenton Varda Apr 13 '20 at 14:33