[Issue 9506] When using alias this, writeln modifies its argument

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Feb 8 00:14:16 PST 2014


https://d.puremagic.com/issues/show_bug.cgi?id=9506



--- Comment #10 from Andrej Mitrovic <andrej.mitrovich at gmail.com> 2014-02-08 00:14:07 PST ---
(In reply to comment #9)
> (In reply to comment #8)
> > Eh, shouldn't it just call t.toString() without even needing to look at alias
> > this? I'm leaning toward either phobos bug or template
> > specialization/constraint change.
> 
> Due to the alias this, the class is a range, so it calls the range version of
> writeln.

Since a recent compiler enhancement it is easy to extract 'alias this'
information from an aggregate. writeln() should do the sane thing and check
this before attempting to consume the range. This might be special behavior,
but writeln() should IMHO not have side-effects. Here's an approach:

-----
static import std.stdio;
import std.range;

void writeln(T)(T t)
{
    static if (isInputRange!(typeof(__traits(getMember, T,
__traits(getAliasThis, T)))))
    {
        auto arr = t[];  // slice it
        std.stdio.writeln(arr);
    }
}

class C
{
    uint[] arr;
    alias arr this;

    this()
    {
        arr = [1, 2];
    }
}

void main()
{
    C c = new C();
    writeln(c);
    writeln(c);
}
-----

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


More information about the Digitalmars-d-bugs mailing list