[Issue 9506] When using alias this, writeln modifies its argument
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Fri Nov 17 14:05:43 UTC 2017
https://issues.dlang.org/show_bug.cgi?id=9506
Steven Schveighoffer <schveiguy at yahoo.com> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|NEW |RESOLVED
Resolution|--- |INVALID
--- Comment #16 from Steven Schveighoffer <schveiguy at yahoo.com> ---
OK, people may not like this, but this is actually expected and defined
behavior. From here: https://dlang.org/phobos/std_format.html#.formatValue.10
For the class objects which have input range interface,
* If the instance toString has overridden Object.toString, it is used.
* Otherwise, the objects are formatted as input range.
The example in question *does* have the input range interface, which is defined
as follows:
is(typeof(R.init) == R)
&& is(ReturnType!((R r) => r.empty) == bool)
&& is(typeof((return ref R r) => r.front))
&& !is(ReturnType!((R r) => r.front) == void)
&& is(typeof((R r) => r.popFront));
And it does not define toString. So format is going to defer to the range
interface, which in this case, passes the int[] by reference essentially.
formatValue was deliberately written this way, so I can't see how it's a bug.
(In reply to Peter Alexander from 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.
In fact, it properly calls the class version of writeln (or formatValue), and
this in turn checks for the overloading of toString (at runtime, BTW), and then
ends up calling the range version. If you alias this it to any other types and
don't override toString, it calls the appropriate format function for that.
See the code here:
https://github.com/dlang/phobos/blob/ea1e9b2aa3a93780adfff928e8acc629b8db9627/std/format.d#L3448
--
More information about the Digitalmars-d-bugs
mailing list