Type inference in array literals
Philippe Sigaud
philippe.sigaud at gmail.com
Sat Aug 28 13:35:25 PDT 2010
On Sat, Aug 28, 2010 at 18:19, Jesse Phillips
<jessekphillips+D at gmail.com<jessekphillips%2BD at gmail.com>
> wrote:
> D uses the last element in the array literal to select the type. I think
> there was a discussion on why a common type wasn't used, but don't remember
> the conclusion. It would be nice if it used the item you are assigning to
> and check they can convert to it.
>
No no, it has been changed months ago. It indeed computes the common type,
for numeric types at least
auto a = [1, 2u, 3.45678, 9]; // a is a double[]
I think it does this with the ?: ternary operator.
As for kickverbot question, I think it's a bug. You should see if it's
already in bugzilla, or report it.
std.traits.CommonType works, though:
alias CommonType!(A,B) O;
writeln(O.stringof); // "Object"
and also the ?: operator:
writeln(typeof(true ? new A() : new B()).stringof); // "Object"
Philippe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20100828/d44a1122/attachment.html>
More information about the Digitalmars-d
mailing list