Target typed new expressions for D?

Paul Backus snarwin at gmail.com
Sat May 1 00:13:08 UTC 2021


On Friday, 30 April 2021 at 16:20:13 UTC, sighoya wrote:
> Relates to the following:
>
> https://devblogs.microsoft.com/dotnet/welcome-to-c-9-0/#target-typed-new-expressions
>
> What do you think about?

I'm not a fan of expressions that require context to determine 
their type.

D already has a few of these for built-in types; for example:

     int[] a1 = [1, 2, 3];
     double[] a2 = [1, 2, 3];

The issue is that, while `[1, 2, 3]` can be either an `int[]` or 
a `double[]` in "normal" code, it's *always* an `int[]` in 
generic code. So if you write something as simple as

     int[3] a1 = [1, 2, 3].staticArray;
     double[3] a2 = [1, 2, 3].staticArray;

...you get a type error:

     Error: cannot implicitly convert expression staticArray([1, 
2, 3]) of type int[3] to double[]


More information about the Digitalmars-d mailing list