Initializing a table of delegates with no-op stubs

H. S. Teoh hsteoh at
Thu Jan 9 22:02:36 UTC 2020

I have a struct containing a bunch of delegates:

	struct S {
		void delegate(int blah) dg1;
		void delegate(string blah) dg2;
		void delegate(int x, int y) dg3;
		... // lots of these

How do I initialize them to no-op stubs?  I've tried all sorts of ways
but couldn't get it to work.  It's easy to do with a *function pointer*
-- just declare a dummy no-op function with the right signature and take
its address:

	void dummy(int) {}
	struct S {
		void function(int) f1 = &dummy; // OK

		// But it doesn't work with delegates, no way, no how:
		void delegate(int) dg1 = &dummy; // NG
		void delegate(int) dg2 = (int) {}; // NG
		void delegate(int) dg3 = (int) => dummy(0); // NG

I even tried to make a function that returns an empty delegate, but when
I try to assign that to the struct member the compiler complains that
it's a non-constant expression.

Seriously, why is it so hard to initialize a delegate field to point to
a 'ret' instruction?!  That's all I need.  It doesn't even need a
context pointer, since it's supposed to do nothing.  Since it's possible
to initialize struct fields to function pointers, surely it can't be
*that* hard to also initialize them to point to empty delegates?

The only success I have is to defer to runtime, and even then it
involves a whole mouthful of periphrasis:

	S s;
	foreach (ref dg; s.tupleof)
		import std.traits : Parameters;
		void impl(size_t i)(Parameters!(typeof(dg))) {}
		dg = &impl;

Am I missing something obvious??


Never ascribe to malice that which is adequately explained by incompetence. -- Napoleon Bonaparte

More information about the Digitalmars-d mailing list