This is called "generics covariance".
It does seem like covariance could be supported here, however this can be
trickier than it looks. It's OK here specifically because constants are
transitively immutable, and can't possibly contain capabilities.
Covariance would be technically incorrect in the presence of capabilities,
if the type parameter is used as the input type to a method. For example:
interface Queue(T) {
add @0 (value :T);
}
const textQueue :Queue(Text) = ...;
const genericQueue :Queue = .textQueue;
// ERROR: genericQueue could be used to send non-text values to
textQueue,
// violating its type contract!
In fact, in this case, *contravariance* could be allowed:
const genericQueue :Queue = ...;
const textQueue :Queue(Text) = genericQueue;
// OK: genericQueue accepts any input, including Text.
Anyway, I *guess* given that there's no such thing as a constant capability
currently, we don't need to worry about that? And covariance is correct for
all other types? So we could support it?
-Kenton
On Mon, Sep 16, 2019 at 11:39 AM <[email protected]> wrote:
> Is this a feature or a bug?
>
> struct Outer {
> inner @0 :Inner;
> }
>
> struct Inner(T) {
> value @0 :T;
> }
>
> struct ValueA { a @0 :Int32; }
> struct ValueB { b @0 :Int32; }
>
> const outerWorks :Outer = (inner = .innerWorks);
> const innerWorks :Inner = ();
>
> const outer :Outer = (inner = .inner); # fails here
> const inner :Inner(Value) = (value = ());
>
> Fails with error: Type mismatch; expected Inner.
>
> I'd like at minimum the above to work, but ideally I would really like is
> something like:
>
> struct OuterList {
> inners @0 :List(Inner);
> }
>
> const outer :OuterList = [
> (inner = :Inner(ValueA) (a = 0)),
> (inner = :Inner(ValueB) (b = 42)),
> ];
>
> Any thoughts? I could take a stab at implementing whatever you suggest. -
> Kuba
>
> --
> 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/ab0c1e68-f52e-46aa-b4e5-c51a113358c1%40googlegroups.com
> <https://groups.google.com/d/msgid/capnproto/ab0c1e68-f52e-46aa-b4e5-c51a113358c1%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/CAJouXQkRQzkHVMo4SgE3npdHaF7KnRxgax1JpYZhmj-jSysM0A%40mail.gmail.com.