Why is this allowed

Steven Schveighoffer schveiguy at gmail.com
Tue Jun 30 18:06:13 UTC 2020


On 6/30/20 12:37 PM, Steven Schveighoffer wrote:
> On 6/30/20 12:22 PM, JN wrote:
>> Spent some time debugging because I didn't notice it at first, 
>> essentially something like this:
>>
>> int[3] foo = [1, 2, 3];
>> foo = 5;
>> writeln(foo);   // 5, 5, 5
>>
>> Why does such code compile? I don't think this should be permitted, 
>> because it's easy to make a mistake (when you wanted foo[index] but 
>> forgot the []). If someone wants to assign a value to every element 
>> they could do foo[] = 5; instead which is explicit.
> 
> That's a feature. I don't think it's going away. The problem of 
> accidental assignment is probably not very common.

I take it back, I didn't realize this wasn't something that happened 
with dynamic arrays:

int[] dyn = [1, 2, 3];

dyn = 5; // error
dyn[] = 5; // ok, brackets required

I would say that's a decent precedent to deprecate and remove that 
functionality.

-Steve


More information about the Digitalmars-d-learn mailing list