Showing a user specified error message when no overloads match

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jul 29 12:28:38 PDT 2014


On Tue, Jul 29, 2014 at 06:57:00PM +0200, Artur Skawina via Digitalmars-d-learn wrote:
> On 07/29/14 17:45, H. S. Teoh via Digitalmars-d-learn wrote:
> > You're right, opDispatch behaves like SFINAE. I've had trouble
> > debugging it before, because when it works, it works very well, but
> > when you accidentally make a typo, it just "disappears" -- you get
> > an error that the property is missing, but the actual error inside
> > opDispatch has been gagged and it's almost impossible to get at the
> > actual error message.
> 
> D's overloaded operators are (usually) normal (templated) functions,
> you can use `a.opDispatch!"blah"` instead of `a.blah` to see what's
> wrong.

Good idea! Though it's still limited when the opDispatch call is buried
under several layers of templated generic functions. The problem is,
sometimes you don't even know that the problem is inside opDispatch
because failure causes it to default to something else (that generates
an unrelated error message), and the actual error message is gagged.
Then once you do narrow it down, it isn't always so easy to get at the
code that calls opDispatch, since it could be quite deep inside generic
functions. Once you pinpoint it, though, your idea is pretty good --
invoke it directly so that the errors are not gagged.


> What's really nasty is the way phobos handles `toString` - if that
> method fails to compile then you get a usually not very helpful
> default, and no warning that something is wrong. It's easy to break
> `toString` w/o noticing anything. Figuring out later what exactly
> broke can be "interesting". Still doable via the above mentioned
> trick, but you'll need to create a mock `sink` etc.
[...]

Mock sinks are very easy, because a delegate is a sink. :-)

	import std.range;
	int delegate(const(char)[]) dg;
	static assert(isOutputRange!(typeof(dg), const(char)[])); // passes

Which makes it very handy to insert arbitrary debugging code for
toString via dg.formattedWrite("%s", obj).


T

-- 
"I'm running Windows '98."
"Yes."
"My computer isn't working now."
"Yes, you already said that."
-- User-Friendly


More information about the Digitalmars-d-learn mailing list