[Issue 9481] New: writeln and alias this

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Feb 8 04:20:35 PST 2013


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

           Summary: writeln and alias this
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: trivial
          Priority: P2
         Component: Phobos
        AssignedTo: nobody at puremagic.com
        ReportedBy: monarchdodra at gmail.com


--- Comment #0 from monarchdodra at gmail.com 2013-02-08 04:20:34 PST ---
The problem, basically, is that when printing a something, if it has an alias
this that has toString printable, then it prints that. EG:

//----
struct S1
{
    int i;
    S2 get()
    {
        return S2();
    }
    alias this = get;
}

struct S2
{
    const void toString(scope void delegate(const(char)[]) sink)
    {
        sink("I'm a S2!!!");
    }
}

void main()
{
    S1 s1 = S1(5);
    writeln(s1);
}
//----

Produces:
I'm a S2!!!

When really, I'd have expected:
S1(5)

The problem comes from "hasToString", which mostly checks if "val.toString" is
legal, but not if the actual type T *has* the member "toString".

The templates then get confused into thinking the type defines "toString", call
"val.toString", which triggers an incorrect call to an alias this cast.

--------

I wrote the corrected code and unittests already, so assigning to self.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list