How to turn an inout(Object) into a string

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Apr 25 21:52:24 PDT 2015


On Sunday, April 26, 2015 03:51:25 Meta via Digitalmars-d-learn wrote:
> On Sunday, 26 April 2015 at 03:48:00 UTC, tcak wrote:
> > On Sunday, 26 April 2015 at 03:09:17 UTC, Meta wrote:
> >> The following code spits out pages of error messages, and it's
> >> driving me insane. I know that Object.toString only has a
> >> mutable variant, so how am I supposed to use writeln,
> >> toString, etc. with an inout, const, or immutable object?
> >>
> >> void main(inout string[] args)
> >> {
> >>    import std.stdio;
> >>
> >>    immutable(Object) o = new immutable Object();
> >>        //Compile error: template instance
> >>        //std.format.formatGeneric!(
> >>        //        LockingTextWriter, immutable(Object),
> >>        //        char)
> >>        //error instantiating
> >>    writeln(o);
> >> }
> >
> > writeln( cast(Object)o );
> >
> > Tada!!
>
> Also undefined behaviour, AFAIK.

It's only undefined if mutation is involved, though I don't know if mutation
is involved in this case or not (I wouldn't think so, but I don't know), and
in general, you definitely shouldn't be casting away const. The real problem
here is likely that toString on Object isn't const, and we can't make it
const. The fix for that which was decided upon was to remove toString,
toHash, opCmp, and opEquals from Object altogether, since we have proper
templates in D and don't really need to put any of those on Object directly
(in which case printing out a plain Object wouldn't work at all anyway), but
there's a fair bit to do before those changes can be made, and
unfortunately, not much of the work for it has been done yet.

https://issues.dlang.org/show_bug.cgi?id=9769
https://issues.dlang.org/show_bug.cgi?id=9770
https://issues.dlang.org/show_bug.cgi?id=9771
https://issues.dlang.org/show_bug.cgi?id=9772

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list