Code repetition

Adam D. Ruppe destructionator at gmail.com
Sun May 27 13:20:08 UTC 2018


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();
}
---

> A string mixin is too messy since it treats the code as a 
> string losing all syntax highlighting, etc.

Configure your editor so the q{ code... } strings don't highlight 
as a string and you get that at least.


More information about the Digitalmars-d-learn mailing list