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

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Feb 8 11:03:22 PST 2014


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



--- Comment #13 from Peter Alexander <peter.alexander.au at gmail.com> 2014-02-08 11:03:12 PST ---
Hmm, I've been looking into this more, and it appears the behaviour may never
be stable across releases.

Consider this:

class A
{
    int[] a = [1, 2, 3];
    alias a this;
}

void main()
{
    auto a = new A();
    foo(a);
}

void foo(R)(R r)
{
    writeln(r);
}


All is fine here. R = A will be deduced, and foo will consume the range as I
have explained.

Now, suppose a specialisation/overload is added for foo:

void foo(int[] r)
{
    writeln(r);
}

Shouldn't change anything right? It does the same thing as the original foo,
just with a more specific parameter type.

The problem is now the array is passed in directly by value, which means the
range is saved. foo(int[]) is more specialised than foo(R) so the implicit
conversion happens and the copy is made. Now, the range will not be consumed.

In summary, if you have a class that uses alias this on a value type range then
you cannot be sure if the range will be consumed or not: it depends what
overloads and specialisations exist :-(

-- 
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