int[] as constructor

Steven Schveighoffer schveiguy at gmail.com
Wed Dec 5 17:00:32 UTC 2018


On 12/5/18 5:34 AM, Stanislav Blinov wrote:
> On Tuesday, 4 December 2018 at 23:28:42 UTC, H. S. Teoh wrote:
> 
>> Well OK, for int[] it's kinda silly 'cos that's the default, but in my 
>> code I've often had to write things like:
>>
>>     auto z = cast(float[]) [ 1.0, 2.0, 3.0 ];
> 
> Err,
> 
> auto z = [ 1.0f, 2, 3 ];
> 
> ?

But that's only because 2 promotes to float. If it's 2.0 (or let's make 
it 2.1 so you can't have the int cop-out), then you have to tag all the 
elements (a bit annoying).

However, you can also do

float[] z = [1.0, 2.0, 3.0];

which is what I'd prefer, and isn't as verbose as either the cast or the 
OP's suggestion. But in cases where you aren't assigning a variable, 
float[](1.0, 2.1, 3.5) would be more desirable than casting (since 
casting is dangerous).

I would say we should allow such usage. And it doesn't conflict with 
array multi-dimensional allocation, since that accepts not a literal, 
but an argument list of sizes.

-Steve


More information about the Digitalmars-d-learn mailing list