[Article Submission] Have Your Efficiency, and Flexibility Too
Nick Sabalausky
a at a.a
Tue May 31 15:48:00 PDT 2011
"Timon Gehr" <timon.gehr at gmx.ch> wrote in message
news:is2lts$2fcn$1 at digitalmars.com...
> bearophile wrote:
>> ...
>> A shorter way to write it:
>>
>> void addGizmos(int numPorts, bool isSpinnable, int numGizmos) {
>> foreach (np; TypeTuple!(1, 2, 3, 5, 10))
>> if (numPorts == np) {
>> foreach (b; TypeTuple!(true, false))
>> if (isSpinnable == b)
>> addGizmosTo!(np, b)(numGizmos);
>> return;
>> }
>>
>> throw new Exception(text(numPorts) ~ "-port Gizmo not supported.");
>> }
>>
>> Bye,
>> bearophile
>
> Nice, but isSpinnable is always checked twice with your approach. Better:
>
> void addGizmos(int numPorts, bool isSpinnable, int numGizmos) {
> foreach (np; TypeTuple!(1, 2, 3, 5, 10))
> if (numPorts == np) {
> if (isSpinnable) addGizmosTo!(np, true)(numGizmos);
> else addGizmosTo!(np, false)(numGizmos);
> return;
> }
>
> throw new Exception(text(numPorts) ~ "-port Gizmo not supported.");
> }
>
I love this idea. I had no idea you could use TypeTuple for values. But it
doesn't seem to be working in ths particular case. On the line that tries to
call 'addGizmosTo' I get this compiler error:
Error: template instance cannot use local 'np' as parameter to non-global
template addGizmosTo(int numPorts,bool isSpinnable)
More information about the Digitalmars-d
mailing list