[Article Submission] Have Your Efficiency, and Flexibility Too

Timon Gehr timon.gehr at gmx.ch
Tue May 31 16:00:38 PDT 2011


Nick Sabalausky wrote:
>"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)
>

Interesting. I suspect that is a bug. Can somebody shed light on what the purpose
of that Error message is?

Timon


More information about the Digitalmars-d mailing list