Thread overview
Array type inference annoyance
Nov 21, 2013
H. S. Teoh
Nov 21, 2013
Jesse Phillips
Nov 21, 2013
Daniel Murphy
November 21, 2013
	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
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
> 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