Why is this allowed

tsbockman thomas.bockman at gmail.com
Wed Jul 1 20:05:51 UTC 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


More information about the Digitalmars-d-learn mailing list