www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 2482] New: Spec does not reference about special x functions in TypeInfo_Struct

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

           Summary: Spec does not reference about special x functions in
                    TypeInfo_Struct
           Product: D
           Version: 2.019
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Keywords: spec
          Severity: normal
          Priority: P2
         Component: www.digitalmars.com
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: schveiguy yahoo.com


The compiler treats certain operator overloads in structs in a special manner. 
Specifically, if an opCmp, opEquals, toHash or toString function matches a
specific signature, a function pointer to that function is stored in the
corresponding TypeInfo_Struct for that struct.

However, if the signatures are not correct, then the function pointer is not
copied.  The result is that certain builtin functions or types (or user types
which use the typeinfo to get at such functions) are not calling the
user-defined functions accessed through the TypeInfo_Struct members.

Nowhere in the spec does it mention this, or have a list of what those
signatures should be.  Also, some of the spec's examples use the wrong
signature for examples of those functions.  For instance, the opCmp example for
structs:

struct Pair
{
    int a, b;
    int opCmp(Pair rhs)
    {
        if (a!=rhs.a) return a-rhs.a;
        return b-rhs.b;
    }
}

This opCmp would *not* be used to sort an array of Pair types.

I think the spec should outline the four special functions mentioned above
(opCmp, opEquals, toHash, toString), and what specific signature they should be
for the compiler to recognize them.  Also, the above example should be updated
to reflect the correct version:

struct Pair
{
    int a, b;
    int opCmp(const Pair *rhs) const
    {
        if (a!=rhs.a) return a-rhs.a;
        return b-rhs.b;
    }
}


-- 
Dec 01 2008