Is this possible in D?

Mike Parker via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Feb 19 04:16:13 PST 2015


On Thursday, 19 February 2015 at 10:17:47 UTC, Dicebot wrote:
> Most practical approach I am currently aware of is wrapping 
> actual implementation (in most restrictive version):
>
I really like mixins for this sort of thing.

```
enum signature = "void longFunction()";
version( Static )
	enum sig = "static " ~ signature;
else
	alias sig = signature;

enum funcBody =
q{{
import std.stdio : writeln;
writeln( "Hi there!" );
version( Static ) writeln( "I'm static!" );
}};

struct Mixed {
	mixin(sig ~ funcBody);
}

void main() {
	version( Static ) Mixed.longFunction();
	else {
		Mixed m;
		m.longFunction();
	}
}
```

@OP: By using a token string (q{}) for funcBody rather than a 
WYSIWYG string (r"" or ``), you can still get syntax highlighting 
in your editor.


More information about the Digitalmars-d-learn mailing list