www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 1457] New: array extension member syntax confused with local member functions

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1457

           Summary: array extension member syntax confused with local member
                    functions
           Product: D
           Version: 1.020
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: wbaxter gmail.com


If you have an extension method for an array and try to call it from within a
class or struct,   foo.bar will be looked up as if it were a call to
this.bar(foo) instead of .bar(foo).  Now if bar were a static method of the
class/struct, there might be a justification for that behavior, but for a
normal instance member I don't think there's any good reason to interpret
foo.bar as this.bar(foo), and it seriously interferes with the goal of adapting
built-in types to existing interfaces.

-- test case below --
(think of it as a primitive attempt to support C++-like iterator syntax with D
arrays)

int begin(T)(T[] x) {
    return 0;
}

struct Hi
{
    int begin() {
        assert(false, "I told you not to call me here!");
    }
    void smell() {
        float[] numbers = [1.0f,2,3,4,5];
        for (int i=numbers.begin(); i<numbers.length; i++) {

        }
    }

}
void main() {
    Hi you;
    you.smell;

//arrayover.d(27): function arrayover.Hi.begin () does not match parameter
types (float[])
//arrayover.d(27): Error: expected 0 arguments, not 1


-- 
Aug 30 2007
parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1457


smjg iname.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |smjg iname.com
           Keywords|                            |rejects-valid




------- Comment #1 from smjg iname.com  2007-10-21 14:27 -------
I've a feeling that there's been something reported to this effect before.  The
problem only occurs because the compiler tries to convert numbers.begin() to
begin(numbers) and then look up the symbol in the local scope.

Really, it doesn't make sense.  Hi.begin is a property of the struct Hi, not of
any array.  So the compiler shouldn't try to match the array property to it. 
Another way to put it is that numbers.begin() should instead be converted to
.begin(numbers).


-- 
Oct 21 2007