[Issue 1323] New: Implement opIn_r for arrays

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Jul 8 17:08:24 PDT 2007


http://d.puremagic.com/issues/show_bug.cgi?id=1323

           Summary: Implement opIn_r for arrays
           Product: D
           Version: 2.002
          Platform: PC
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla at digitalmars.com
        ReportedBy: wbaxter at gmail.com


int[] someInts = getInts();
if (0 in someInts) {
   handleZero(someInts);
}

>> ERROR

I just did this again for the umpteenth time so I thought I'd go ahead and post
it as an enhancement request.

In Python at least the 'in' operator works on any sequence type.  There's no
reason why it shouldn't work on regular arrays in D too.  The implementation
should basically be the linear search equivalent of what associative arrays do.
 Something like:

T* opIn_r(T searchval) {
 for(size_t i=0; i<array.length; i++) {
    if (array[i]==searchval) { return &array[i]; }
 }
 return null;
}

Maybe == might not be the right equality check to use (since that would fail
for an array with nulls).  Maybe it should be specialized depending on whether
the elements are build-in/class/struct.  But the idea is that "x in array"
should do /something/ useful.

--bb


-- 



More information about the Digitalmars-d-bugs mailing list