On Friday, 21 November 2014 at 13:39:43 UTC, monarch_dodra wrote:
I trust everyone here knows about the "builder" pattern
(http://en.wikipedia.org/wiki/Builder_pattern)? It can be very
useful when the number of (optional) arguments in a function
start to run rampant, and you know the user only wants to start
setting a subset of these.

D has phenomenal meta programming possibilities, and I see more
and more templates taking more and more parameters. So I thought
to myself, why not have a template builder pattern?

I was able to throw this together:
http://dpaste.dzfl.pl/366d1fc22c9c

Which allows things like:
        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".

I'm also wondering if there is prior "literature" about this, and
if it's something we'd want more of in Phobos?

That's pretty awesome. There have been several times where I've been put off adding template parameters when I really wanted to, simply because I didn't want to have long, hard-to-read parameter lists in user code.

Ideally I would like to be able to do this:
mixin TemplateBuilder!(MyType, PresetTemplateArgs) MyChoiceOfBuilderName; where preset template args is optional and variadic, e.g. T in your example.

Reply via email to