Advertisement
Guest User

isIterable redux

a guest
Sep 30th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// Determine whether some type can be iterated over using foreach.
  2. /// Will not evaluate true for types such as tuples where all elements may
  3. /// not be of the same type.
  4. enum bool isIterable(alias T) = isIterable!(typeof(T));
  5. /// ditto
  6. enum bool isIterable(T) = (
  7.     is(typeof({
  8.         // Must be iterable
  9.         foreach(e; T.init){}
  10.         // Indexes must not be known at compile-time
  11.         // Because if they are, this is a tuple
  12.         static if(is(typeof({foreach(i, e; T.init){}}))){
  13.             foreach(i, e; T.init){
  14.                 static assert(!is(typeof({enum x = i;})));
  15.             }
  16.         }
  17.     })) && !is(typeof({
  18.         // Must not be an empty tuple
  19.         foreach(e; T.init){
  20.             static assert(false);
  21.         }
  22.     }))
  23. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement