"Named parameter builder" pattern for template parameters

Robik via Digitalmars-d digitalmars-d at puremagic.com
Wed Nov 26 09:42:06 PST 2014


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?

If D would support getting parameter names(currently does not 
work for lambdas) of lambdas we could have:

    someConnect(host => "test", port => 7999);

Just a random thought :)


More information about the Digitalmars-d mailing list