Deduplicating template reflection code
Johannes Pfau via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Apr 14 04:29:03 PDT 2017
Am Fri, 14 Apr 2017 08:55:48 +0000
schrieb Moritz Maxeiner <moritz at ucworks.org>:
>
> mixin Foo!(API, (MethodType) {
> // function dependent code here
> });
> foo();
> ---
>
> Option 2: Code generation using CTFE
>
> ---
> string genFoo(alias API, string justDoIt)
> {
> import std.array : appender;
> auto code = appender!string;
> code.put(`[...]1`);
> code.put(`foreach (MethodType; overloads) {`);
> code.put(justDoIt);
> code put(`}`);
> code.put(`[...]2`);
> }
>
> mixin(genFoo!(API, q{
> // function dependent code here
> })());
> ---
>
> Personally, I'd consider the second approach to be idiomatic, but
> YMMW.
I'd prefer the first approach, simply to avoid string mixins. I think
these can often get ugly ;-)
Is there some way to wrap the 'type selection'? In pseudo-code something
like this:
enum FilteredOverloads(API) = ...
foreach(Overload, FilteredOverloads!API)
{
....
}
-- Johannes
More information about the Digitalmars-d-learn
mailing list