If you have a struct Foo whose fields are a strict superset of some other
struct Bar (with matching field numbers and everything), then you can
convert one to the other like:

    Foo::Reader foo = ...;
    Bar::Reader bar = capnp::AnyStruct::Reader(foo).as<Bar>();

(Or vice versa.)

But the big problem with this is that you can't ever add a new field to
Bar, because it would force you to renumber all the later fields of Foo,
which would be a backwards-incompatible change.

In general I instead recommend that Foo should contain a field of type Bar.
This seems to suit most use cases just fine.

BTW, note that RPC interfaces in Cap'n Proto do support inheritance. It's
only structs that don't.

-Kenton

On Sun, Jul 19, 2020 at 11:08 PM <[email protected]> wrote:

>
> I understand real inheritance is a hornets nest or complex weirdness, and
> I don't need that.
>
> but could we add a really simple version?
>
> like all the msgs have a common 5 fields in them. make the common fields
> an interface that all my msgs inherit from?
>
> I don't expect to ever instantiate a concrete version of the "base" class.
> I just want to pass the derived msgs to a function that expects the
> base-type.
>
> --
> 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].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/capnproto/e08ca0bc-b386-4d04-b77d-569be721009do%40googlegroups.com
> <https://groups.google.com/d/msgid/capnproto/e08ca0bc-b386-4d04-b77d-569be721009do%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/capnproto/CAJouXQ%3DxDvfpXZ-WKvQ9EK%3DYwE%2BV%2Bu2QYAeLYWK1B-yL4osXAg%40mail.gmail.com.

Reply via email to