"Named parameter builder" pattern for template parameters

monarch_dodra via Digitalmars-d digitalmars-d at puremagic.com
Fri Nov 21 05:39:42 PST 2014


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?


More information about the Digitalmars-d mailing list