www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Array type inference annoyance

reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
	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
next sibling parent "Jesse Phillips" <Jesse.K.Phillips+D gmail.com> writes:
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
Nov 20 2013
prev sibling parent "Daniel Murphy" <yebblies nospamgmail.com> writes:
 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