July 12, 2013 [Issue 10625] New: Compiler should warn or disallow using slice syntax in initialization | ||||
---|---|---|---|---|
| ||||
http://d.puremagic.com/issues/show_bug.cgi?id=10625 Summary: Compiler should warn or disallow using slice syntax in initialization Product: D Version: D2 Platform: All OS/Version: All Status: NEW Keywords: accepts-invalid Severity: normal Priority: P2 Component: DMD AssignedTo: nobody@puremagic.com ReportedBy: andrej.mitrovich@gmail.com --- Comment #0 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-07-12 09:57:21 PDT --- ----- void main() { int[] source = [0, 1]; int[] arr1 = new int[](2); arr1[] = source[]; assert(arr1 !is source); // ok int[] arr2 = source[]; // looks like a copy assert(arr2 !is arr2); // assert fails: but in fact it's not! } ----- The 'int[] arr2 = source[];' syntax appears as though the source contents are copied into arr2, but in fact this is the same code as 'int[] arr2 = source;'. Since [] has a very special meaning, the above should either: 1) Become an actual deep copy, meaning arr2 would have to allocate space first and then copy contents. This would be a breaking and negative change due to performance implications. 2) Not compile. It looks like a deep copy but it isn't, and this can cause issues down the road (for example using memcpy or even OpenGL functions can create hard to track problems due to using the source and target arrays which point to the same memory). I'd vote heavily towards 2. Of course one can always use 'int[] arr2 = source.dup'. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
July 12, 2013 [Issue 10625] Compiler should warn or disallow using slice syntax in initialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | http://d.puremagic.com/issues/show_bug.cgi?id=10625 bearophile_hugs@eml.cc changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |bearophile_hugs@eml.cc --- Comment #1 from bearophile_hugs@eml.cc 2013-07-12 10:33:05 PDT --- Related to Issue 7445 ? -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
Copyright © 1999-2021 by the D Language Foundation