Initializing a table of delegates with no-op stubs

H. S. Teoh hsteoh at
Mon Jan 13 18:06:23 UTC 2020

On Sat, Jan 11, 2020 at 09:51:24AM -0500, Steven Schveighoffer via Digitalmars-d wrote:
> On 1/11/20 4:40 AM, Rainer Schuetze wrote:
> > void noop(int) {}
> > 
> > enum void delegate(int) dg_noop = (int x){ noop(x); };
> No need for the function call
> enum void delegate(int) dg_noop = (int x) {};

Mmm, very nice!  Kind of a weird paraphrasis, but at least it works. To
minimize hassle, I turned it into a template:

	enum void delegate(Args) doNothing(Args...) = (Args args) {};

	struct S {
		void delegate(int x) dg1 = doNothing!(int);
		void delegate(float y) dg2 = doNothing!(float);
		void delegate(int x, int y) dg3 = doNothing!(int,int);

Do you think the first line is worth an addition to Phobos, maybe
alongside toDelegate?  It's non-obvious enough that it might save a
newbie (or a not-so-newbie like myself :-P) a lot of headache.


Political correctness: socially-sanctioned hypocrisy.

More information about the Digitalmars-d mailing list