July 01, 2020
On Tuesday, 30 June 2020 at 16:22:57 UTC, JN wrote:
> 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.

Totally agree. In most of cases implicit actions lead to errors. Even if they have a specific use case and really convenient.
July 01, 2020
On Tuesday, 30 June 2020 at 16:22:57 UTC, 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.

What's your opinion on using that syntax in the initial declaration, like `float[16] foo = 0`?
July 01, 2020
On 7/1/20 11:57 AM, Nathan S. wrote:
> On Tuesday, 30 June 2020 at 16:22:57 UTC, 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.
> 
> What's your opinion on using that syntax in the initial declaration, like `float[16] foo = 0`?

It's important to keep at least something that allows such setting. It would be reasonable to do this with a function as well.

Is it possible to have the initialization syntax work differently from the assignment (i.e. allow the initialization as above, but require the brackets for assignment)?

-Steve
July 01, 2020
On Wednesday, 1 July 2020 at 15:57:24 UTC, Nathan S. wrote:
> On Tuesday, 30 June 2020 at 16:22:57 UTC, 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.
>
> What's your opinion on using that syntax in the initial declaration, like `float[16] foo = 0`?

I don't like it. I'd prefer:

float[16] foo = [ 0 ];

or

float[16] foo = { 0 };

or

float[16] foo(0);
July 01, 2020
On Tuesday, 30 June 2020 at 16:36:45 UTC, H. S. Teoh wrote:
> And on that note, this implicit static -> dynamic array conversion is seriously a nasty misfeature that ought to be killed with fire. It leads to bugs like this:
>
> 	struct Database {
> 		int[] data;
> 		void set(int[] _data) {
> 			data = _data;
> 		}
> 	}
> 	void myFunc(ref Database db) {
> 		int[3] x;
> 		db.set(x);	// oops
> 	}

If you want the compiler to stop you from accidentally keeping references to stack variables past the end of their scope, you need to annotate your functions @safe and compile with -preview=dip1000: https://run.dlang.io/is/3VdDaN

Furthermore, the problem your example shows has nothing to do with implicit static to dynamic array conversion, as without @safe the same error can easily be committed with non-array types: https://run.dlang.io/is/nBjibd
July 01, 2020
On Wednesday, 1 July 2020 at 20:05:51 UTC, tsbockman wrote:
> If you want the compiler to stop you from accidentally keeping references to stack variables past the end of their scope, you need to annotate your functions @safe and compile with -preview=dip1000: https://run.dlang.io/is/3VdDaN
>
> Furthermore, the problem your example shows has nothing to do with implicit static to dynamic array conversion, as without @safe the same error can easily be committed with non-array types: https://run.dlang.io/is/nBjibd

Hmm. Those run.dlang.io short links seem to allow editing of the code, so I'd better paste it here for permanence:

// Compile with -preview=dip1000
struct Database {
    int[] data;
    void set(int[] _data) @safe {
        data = _data;
    }
}
void myFunc(ref Database db) @safe {
    int[3] x;
    db.set(x);	// This is a compile-time error, as it should be.
}

Database theDB;
void main() {
    myFunc(theDB);
}

// This version shows that the problem is not using @safe and dip1000,
// not anything to do with arrays:
struct Database {
    int* data;
    void set(ref int _data) {
        data = &_data;
    }
}
void myFunc(ref Database db) {
    int x;
    db.set(x);	// oops
}

Database theDB;
void main() {
    myFunc(theDB);
}


1 2
Next ›   Last »