Skip to content

Commit f4e8cc4

Browse files
authored
Merge pull request #8819 from atilaneves/isInputRange-element
Add optional element type to isInputRange
2 parents a3f2212 + b39a36a commit f4e8cc4

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

changelog/is_input_range_element.dd

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
isInputRange now takes an optional element type.
2+
3+
isInputRange now has an optional 2nd template parameter that defaults
4+
to void. If not void, it only evaluates to true if the range's element
5+
type is the same type as this extra argument, modulo const. For
6+
instance, `isInputRange!(int[], const(int))` is true, but
7+
`isInputRange!(int[], string)` is false.

std/range/primitives.d

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,17 +165,21 @@ See_Also:
165165
166166
Params:
167167
R = type to be tested
168+
E = the type of the elements of the range if not `void`
168169
169170
Returns:
170-
`true` if R is an input range, `false` if not
171+
`true` if R is an input range (possibly with element type `E`), `false` if not
171172
*/
172-
enum bool isInputRange(R) =
173+
enum bool isInputRange(R, E = void) =
173174
is(typeof(R.init) == R)
174175
&& is(typeof((R r) { return r.empty; } (R.init)) == bool)
175176
&& (is(typeof((return ref R r) => r.front)) || is(typeof(ref (return ref R r) => r.front)))
176177
&& !is(typeof((R r) { return r.front; } (R.init)) == void)
177-
&& is(typeof((R r) => r.popFront));
178-
178+
&& is(typeof((R r) => r.popFront))
179+
&& (is(E == void) ||
180+
is(ElementType!R == E) ||
181+
is(const(ElementType!R) == E) ||
182+
(is(const(ElementType!R) == immutable E) && is(const(E) == E)));
179183
///
180184
@safe unittest
181185
{
@@ -192,6 +196,18 @@ enum bool isInputRange(R) =
192196
static assert( isInputRange!(char[]));
193197
static assert(!isInputRange!(char[4]));
194198
static assert( isInputRange!(inout(int)[]));
199+
static assert(!isInputRange!(int[], string));
200+
static assert( isInputRange!(int[], int));
201+
static assert( isInputRange!(int[], const int));
202+
static assert(!isInputRange!(int[], immutable int));
203+
204+
static assert(!isInputRange!(const(int)[], int));
205+
static assert( isInputRange!(const(int)[], const int));
206+
static assert(!isInputRange!(const(int)[], immutable int));
207+
208+
static assert(!isInputRange!(immutable(int)[], int));
209+
static assert( isInputRange!(immutable(int)[], const int));
210+
static assert( isInputRange!(immutable(int)[], immutable int));
195211

196212
static struct NotDefaultConstructible
197213
{

0 commit comments

Comments
 (0)