SHOW:
|
|
- or go back to the newest paste.
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) = is(typeof({ |
6 | + | enum bool isIterable(T) = ( |
7 | - | foreach(e; T.init){ // Must be iterable |
7 | + | is(typeof({ |
8 | - | foreach(typeof(e) x; T.init){} // All elements must have a common type |
8 | + | // Must be iterable |
9 | - | } |
9 | + | foreach(e; T.init){} |
10 | - | })) && !is(typeof({ |
10 | + | // Indexes must not be known at compile-time |
11 | - | foreach(e; T.init){ |
11 | + | // Because if they are, this is a tuple |
12 | - | static assert(false); // Must not be an empty tuple |
12 | + | static if(is(typeof({foreach(i, e; T.init){}}))){ |
13 | - | } |
13 | + | foreach(i, e; T.init){ |
14 | - | })); |
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 | ); |