[Issue 4733] New: Possible bugs caused by dynamic arrays in boolean evaluation context
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Thu Aug 26 14:12:13 PDT 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4733
Summary: Possible bugs caused by dynamic arrays in boolean
evaluation context
Product: D
Version: D2
Platform: All
OS/Version: All
Status: NEW
Severity: enhancement
Priority: P2
Component: DMD
AssignedTo: nobody at puremagic.com
ReportedBy: bearophile_hugs at eml.cc
--- Comment #0 from bearophile_hugs at eml.cc 2010-08-26 14:12:04 PDT ---
In Python it is a normal idiom to test for collection emptiness just putting
their name in a boolean evaluation context (this is an idom valid for all
Python collections):
arr = [1]
assert arr
But in D similar code causes troubles, this differt code shows an example:
void main() {
int[] arr = [1];
int* orig_ptr = arr.ptr;
arr.length = 0;
assert(!!arr); // this doesn't assert
assert(arr.ptr == orig_ptr); // this doesn't assert
}
!!arr is true because while arr.length is zero, arr.ptr is not null.
To avoid possible bugs (for example for programmers coming from Python
language) I suggest to turn "if(arr)" into a syntax error, forcing to use
"if(arr.length)" or "if(arr.ptr)" or similar things, and you avoid possible
bugs. In the uncommon cases where you want to test both fields to be empty, you
may use "if(arr.length || arr.ptr)" or "if(arr.length != arr.init)" or similar
things.
The semantics of "if(aa)" and "if(static_arr)", the first tests if the
reference is null, the second if all items are empty.
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list