Ideally the boilerplate could be cut down by having a standard type
like:
struct Option(T) {
union {
Some @0 :T;
None @1 :Void;
}
}
And then just do:
struct Foo {
age @0 :Option(Int32);
}
But the wire format doesn't really do what you want; age ends up being a
*pointer* to an option (if I understand correctly). I'm with you that
it would be nice to keep these concepts separate. The above is basically
how programming languages that have variant types (haskell, ocaml,
scala, rust...) deal with the issue (and there's no such thing as a null
pointer, which is amazing). It would be nice if you could define
something like the above and then have the union just inlined into the
parent struct, instead of being a pointer. Maybe by making it possible
to define unions at top-level, but not actually instantiate them unless
they're part of another type. You'd still have the problem of the extra
tag field. Just spitballing.
Quoting Sam Duke (2017-03-09 14:44:30)
> I think the idea of an unset sentinel value is a little awkward as it
> conflates data validation with field presence which are nice concepts
> to keep separate (in part for consistency in how you perform data
> validation).
> As I understand it a union is synonymous to a Protobuf oneof field?
> Is the recommendation to use a union with only one member or use
> wrappers (like this
> [1]https://github.com/google/protobuf/blob/master/src/google/protobuf/w
> rappers.proto) which don't really fix the problem buy minimise it?
> Using unions everywhere would make the definitions of messages hard to
> read. It suppose some syntactic sugar around this might be useful :/
> Thanks for your prompt replies and help BTW. And sorry for my poor
> reading of your docs :)
>
> On Thu, 9 Mar 2017 at 19:15 Ross Light <[2][email protected]> wrote:
>
> Quite the opposite: it's so that scalar values always have a fixed
> offset from the beginning of the struct's data section.� This may
> waste space on the wire, but makes scalar access consistent and avoids
> branches in the common case (read as fast).
> However, it's worth noting that reading a scalar field with a newer
> version of the schema will read the default value, so as the FAQ
> states, you can use this property to have an "unset" sentinel value or
> you can add a named union.
>
> On Thu, Mar 9, 2017 at 10:27 AM Sam Duke <[3][email protected]>
> wrote:
>
> Really confused why this was removed? Is it for less space on the
> wire? Field presence seems an excellent way to guard against
> regression as protos evolve.
>
> On Thu, 9 Mar 2017, 18:20 Ross Light, <[4][email protected]> wrote:
>
> Correct.� In this respect, Cap'n Proto is very close to proto3
> semantics.
>
> On Thu, Mar 9, 2017 at 10:08 AM Sam Duke <[5][email protected]>
> wrote:
>
> Looks like there's no hasX methods for scalars?
>
> On Thu, 9 Mar 2017, 15:57 Ross Light, <[6][email protected]> wrote:
>
> [7]https://capnproto.org/faq.html#how-do-i-make-a-field-optional
>
> On Thu, Mar 9, 2017, 4:01 AM <[8][email protected]> wrote:
>
> I couldn't find an explicit mention of this on the website, curious if
> CapNProto has also removed field presence detection - it's a really
> useful feature to have!
>
> --
> You received this message because you are subscribed to the Google
> Groups "Cap'n Proto" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to [9][email protected].
> Visit this group at [10]https://groups.google.com/group/capnproto.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Cap'n Proto" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to [11][email protected].
> Visit this group at [12]https://groups.google.com/group/capnproto.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Cap'n Proto" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to [13][email protected].
> Visit this group at [14]https://groups.google.com/group/capnproto.
>
> Verweise
>
> 1.
> https://github.com/google/protobuf/blob/master/src/google/protobuf/wrappers.proto
> 2. mailto:[email protected]
> 3. mailto:[email protected]
> 4. mailto:[email protected]
> 5. mailto:[email protected]
> 6. mailto:[email protected]
> 7. https://capnproto.org/faq.html#how-do-i-make-a-field-optional
> 8. mailto:[email protected]
> 9. mailto:[email protected]
> 10. https://groups.google.com/group/capnproto
> 11. mailto:[email protected]
> 12. https://groups.google.com/group/capnproto
> 13. mailto:[email protected]
> 14. https://groups.google.com/group/capnproto
--
You received this message because you are subscribed to the Google Groups
"Cap'n Proto" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
Visit this group at https://groups.google.com/group/capnproto.
signature.asc
Description: signature
