Initializing a table of delegates with no-op stubs

Rainer Schuetze r.sagitario at gmx.de
Mon Jan 13 22:43:22 UTC 2020



On 13/01/2020 19:06, H. S. Teoh wrote:
> 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.
> 

I think it's a bug in the compiler. If you can assign an enum value, why
should the value itself not be good enough aswell?


More information about the Digitalmars-d mailing list