"Timon Gehr" <[email protected]> wrote in message news:[email protected]... > 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)
