digitalmars.D - Array type inference annoyance
- H. S. Teoh (29/29) Nov 20 2013 class Base { ... }
- Jesse Phillips (3/9) Nov 20 2013 I think it is this bug:
- Daniel Murphy (1/5) Nov 21 2013 https://d.puremagic.com/issues/show_bug.cgi?id=5498
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
Nov 20 2013
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. :-( TI think it is this bug: http://d.puremagic.com/issues/show_bug.cgi?id=3543
Nov 20 2013
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
Nov 21 2013