www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 9481] New: writeln and alias this

reply d-bugmail puremagic.com writes:
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 puremagic.com
        ReportedBy: monarchdodra gmail.com


--- Comment #0 from monarchdodra 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: -------
Feb 08 2013
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9481



--- Comment #1 from Kenji Hara <k.hara.pg gmail.com> 2013-02-08 21:47:57 PST ---
Current behavior is consistent with class inheritance.

class B {
    override string toString()
    {
        return "I'm a B!";
    }
}
class C : B {}

void main()
{
    C c = new C();
    B b = new C();
    assert(c.toString() == "I'm a B!");
    assert(b.toString() == "I'm a B!");
}

I think that 'alias this' should work as same as normal class inheritance,
excepting 'alias this' specific cases.

When I refactored std.format, I designed such literal-like formatting (e.g.
"S1(5)") as a *fallback* form. That means: if S1 has toString method (even it
is defined in the 'alias this'ed type S2), it will be always used for the
formatting of S1 object.

So, it is an expected behavior, and there is no deviation from the design.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 08 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9481


Maxim Fomin <maxim maxim-fomin.ru> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|wrong-code                  |
             Status|NEW                         |RESOLVED
                 CC|                            |maxim maxim-fomin.ru
         Resolution|                            |INVALID


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