Code repetition

IntegratedDimensions IntegratedDimensions at gmail.com
Sun May 27 23:15:16 UTC 2018


On Sunday, 27 May 2018 at 13:20:08 UTC, Adam D. Ruppe wrote:
> On Sunday, 27 May 2018 at 06:47:38 UTC, IntegratedDimensions 
> wrote:
>> Putting the code in a template/function/lambda does not work 
>> because of the scopes which will be called when the main 
>> function exists.
>
> I think you might just be using the wrong kind of function.
>
> ---
> import std.stdio;
> // the helper does the setup then calls another function for 
> specific stuff
> void helper(void delegate(ref int) specialized) {
>     // setup stuff
>     int x;
>     scope(exit) writeln("exit ", x);
>
>     // specialized stuff abstracted out
>     specialized(x);
> }
>
> void foo() {
> 	helper( (ref x) {
> 		x = 34;
> 	});
> }
>
> void main() {
> 	foo();
> }
> ---

Yeah, this should work. Was hoping there was just a simple way to 
pull the code out but I guess this provides the best alternative.

Thanks.




More information about the Digitalmars-d-learn mailing list