Code repetition
Malte
no at valid.mail
Sun May 27 11:57:55 UTC 2018
On Sunday, 27 May 2018 at 06:47:38 UTC, IntegratedDimensions
wrote:
> A string mixin is too messy since it treats the code as a
> string losing all syntax highlighting, etc.
>
> I'd love to have something like a template mixin where I can
> just do
>
> mixin template fooSetup(ret)
> {
> // setup stuff
> int x;
> scope(exit) something;
>
> }
>
>
> and
>
> void foo()
> {
> fooSetup(3);
> }
>
>
> void foo4()
> {
> fooSetup(13);
> }
>
> and everything behave as it should. I'm guessing this is going
> to be impossible to achieve in D?
Well, if you want to have the same power as Cs textual
replacement, you need string mixins. Often you can replace it
with templates, but that depends on the application. I would
avoid it if you can though, but more for debugging and not
because of syntax highlighting.
void main()
{
import std.stdio;
int y = 1;
mixin(fooSetup(3));
writeln("x is ",x);
x = 5;
}
string fooSetup(int val) {
import std.conv;
return q{
int x = }~val.to!string~q{;
scope(exit) {
writeln("x was ", x);
writeln("y was ", y);
}
};
}
More information about the Digitalmars-d-learn
mailing list