On Sun, 26 Apr 2009 21:44:31 +0400, dsimcha <[email protected]> wrote:

I've been thinking a little more about ranges, etc. and it would be nice to have a template for isIterable(T) that simply tells whether an object can be iterated over with foreach, without caring how this iteration works (ranges,
opApply, builtin array/AA).  I have some use cases where I'm writing very
generic functionality and all I need is the lowest common denominator that,
given an object T, the following will compile, and to know what type elem
would be:

foreach(elem; T.init) {}

This functionality does not require any of the more advanced features of
ranges, just iteration. Is there any good way to write a template for this? Since foreach is a statement, is(typeof()) and __traits(compiles) are out.

// Not tested
template isIterable(T)
{
   static if (is(typeof({foreach(elem; T.init) {}})) {
       const bool isIterable = true;
   } else {
       const bool isIterable = false;   
   }
}


Reply via email to