"Named parameter builder" pattern for template parameters

Oren Tirosh via Digitalmars-d digitalmars-d at puremagic.com
Sun Nov 23 03:35:29 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?

In this blog post http://tomerfiliba.com/blog/dlang a syntax 
based on overloading opDollar and opDispatch is proposed for 
key/value pairs:

alias MyContainerType = ContainerBuilder!int[
   $.Scan=false, $.SomeOtherThing=true
].Type;


I still can't make up my mind if it's brilliant or blasphemous.


More information about the Digitalmars-d mailing list