Summary: Improving the compiler 'in' associative array can
                    return just a bool
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD

--- Comment #0 from 2010-07-16 16:32:54 PDT ---
This is relative to page 18 and 56 of The D Programming Language.

"foo in associativeArray" returns a pointer. So can this work in SafeD too?
Maybe there are ways to accept this in SafeD too (if the pointer is not used
and just tested if it's null or not), but there is a cleaner alternative

In normal D code there is no need to write this to find the parity of x:
int parity = x & 1;

The following operation can be used, that is more readable, because some stage
of compiler is able to optimize this to the first expression:
int parity = x % 2;

The "in" for associative arrays returns a pointer for efficiency reasons, to
avoid a double lookup in some situations. But the D1 LDC compiler is now be
able to optimize away two "close" associative array lookups in all situations,
performing just one lookup.

LDC is probably not able to perform this optimization if the pointer is stored
in a variable and used much later, but this is not a common usage pattern, so I
think this can be ignored.

If the compiler is able to perform this optimization, there the "in" can return
a boolean, and it can be used cleanly in SafeD code too.

So in this case consider returning a boolean and improving the compiler
instead. DMD is probably currently (v20.47) not able to perform this

Configure issuemail:
------- You are receiving this mail because: -------

Reply via email to