toString

Rainer Schuetze r.sagitario at gmx.de
Mon Dec 31 15:39:05 UTC 2018



On 31/12/2018 11:13, Michelle Long wrote:
> Display variables in the watch shows the variables in a flat list. Can
> we just get an override for toString or some other method to change what
> is displayed?
> 
> Surely this can be accomplished?
> 
> Even if it means creating an external DLL that Visual D can read and
> choose the right function and use it to take the object and return a
> string and simply display that string instead of whatever Visual D is
> generating internally?
> 
> struct S
> {
>    int x;
> }
> 
> 
> Somewhere
> 
>    auto S_toString(S s)
>    {
>       return "S: "~s.x;
>    }
> 
> Should be very simple to do. Visual D just uses that function.
> 
> 

What you can do now when using the VS debugger with the mago extension
(and not mago selected as debug engine):

struct Size
{
	int w;
	int h;

	const char* toDebug()
	{
		import std.conv;
		import std.utf;

		return toUTFz!(char*)(to!string(w) ~ "x" ~ to!string(h));
	}
}

int test()
{
	Size sz = Size(10, 20);
	return sz.w * sz.h; // Breakpoint here
}

Run to the breakpoint and then add watch "sz.toDebug()" to execute the
function and display the result.

Some limitations apply:
- no arguments but the implicit this or context pointer (i.e. delegates
work, too)
- doesn't work with slice return values for x64 due to incompatible ABI
- you have to manually reevaluate in the watch window due to possible
side-effects
- the example above currently seems broken for x86, not sure why


More information about the Digitalmars-d-ide mailing list