Thread overview | |||||
---|---|---|---|---|---|
|
November 21, 2013 Array type inference annoyance | ||||
---|---|---|---|---|
| ||||
class Base { ... }
class Derived1 : Base { ... }
class Derived2 : Base { ... }
void func(Base[] objs) { ... }
func([
new Derived1(),
new Derived2() // OK
]);
Derived2 d2ptr;
func([
new Derived1(),
d2ptr = new Derived2() // NG: compile error (WAT?)
]);
// Workaround:
func([
cast(Base) new Derived1(), // ugh
d2ptr = new Derived2() // OK
]);
According to TDPL (§2.2.6, p.40), the type of array literals are inferred by applying the ?: operator to elements pairwise. But the second call to func() above fails in spite of the fact that this code passes:
static assert(is(typeof(true ? new Derived1() : (d2ptr =
newDerived2())) == Base));
The necessity of the cast as in the workaround is really ugly, and is a fly in my otherwise pleasant D soup currently. :-(
T
--
Talk is cheap. Whining is actually free. -- Lars Wirzenius
|
November 21, 2013 Re: Array type inference annoyance | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | On Thursday, 21 November 2013 at 05:47:54 UTC, H. S. Teoh wrote: > static assert(is(typeof(true ? new Derived1() : (d2ptr = > newDerived2())) == Base)); > > The necessity of the cast as in the workaround is really ugly, and is a > fly in my otherwise pleasant D soup currently. :-( > > > T I think it is this bug: http://d.puremagic.com/issues/show_bug.cgi?id=3543 |
November 21, 2013 Re: Array type inference annoyance | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | > According to TDPL (§2.2.6, p.40), the type of array literals are inferred by applying the ?: operator to elements pairwise. But the second call to func() above fails in spite of the fact that this code passes: https://d.puremagic.com/issues/show_bug.cgi?id=5498 |
Copyright © 1999-2021 by the D Language Foundation