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

--- Comment #0 from 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

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:
------- You are receiving this mail because: -------

Reply via email to