"Named parameter builder" pattern for template parameters
Artur Skawina via Digitalmars-d
digitalmars-d at puremagic.com
Sat Nov 22 08:36:02 PST 2014
On 11/21/14 14:39, monarch_dodra via Digitalmars-d wrote:
> D has phenomenal meta programming possibilities
[...]
> alias MyContainerType = ContainerBuilder!int
> //.setUseGC!??? //Don't care about GCm use default.
> .setScan!false
> .setUSomeOtherThing!true
> .Type;
>
> I think this is hugely neat. I'm posting here both to share, and
> for "peer review feedback".
You can just use a struct and D's phenomenal compile time
programming possibilities. ;)
struct fParms {
int one = 1;
string two = "one";
}
alias fParm = TParm!fParms;
auto f(alias P=fParm)() {
// ... use P.$parm to access parameters ...
pragma(msg, P.one, " ", P.two);
}
auto f2(alias P, A...)(A a) { f!P(); }
auto f3(alias P, A...)(A a) { enum ALT = P.one = P.one+34; f!ALT(); }
void main() {
f();
f!(fParm.one=2)();
f!(fParm.two="three")();
enum a = "four";
f!((fParm.one=4).two=a)();
f!(fParm.one(4).two(a))();
enum b = "five";
f!(fParm.two=b)();
f!(fParm.one(6).two(b~"+1"))();
enum c = "seven";
f2!(fParm.two=c)(42);
f3!(fParm.one=8)(42);
}
@property TParm(alias Parms)() {
static struct FParm {
private Parms _parms;
template opDispatch(string M) {
ref opDispatch(B)(B b) @property {
mixin("_parms."~M) = b;
return this;
}
auto opDispatch()() @property {
return mixin("_parms."~M);
}
}
}
return FParm();
}
artur
More information about the Digitalmars-d
mailing list