Type inference and overloaded functions

Kenji Hara k.hara.pg at gmail.com
Tue Dec 10 02:56:40 PST 2013


On Tuesday, 10 December 2013 at 07:32:08 UTC, Marco Leise wrote:
> [1,2,3] looks like a static array to me. And if overload
> resolution picked the most specialized function it seems
> natural to call the int[3] version.
> My reasoning being that static arrays can be implicitly
> converted to dynamic array, but the reverse is not true. So I
> think it would be better to have [1,2,3] be a static array and
> keep the current behavoir, no?)

In early D1 age, array literals and string literals had had 
static array types which corresponding to the literals' element 
count. However it had caused template code bloat.

void foo(T)(T arg) { ... }

foo("aaa");   // instantiate foo!(char[3])
foo("bbbb");  // instantiate foo!(char[4])

foo([1,2]);    // instantiate foo!(int[2])
foo([1,2,3]);  // instantiate foo!(int[3])

So their types were changed to dynamic array by default.

Kenji Hara


More information about the Digitalmars-d-learn mailing list