Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional element type to isForwardRange #8842

Merged
merged 1 commit into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions changelog/is_forward_range_element.dd
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
isForwardRange now takes an optional element type.

isForwardRange now has an optional 2nd template parameter that defaults
to void. If not void, it only evaluates to true if the range's element
type is the same type as this extra argument, modulo const. For
instance, `isForwardRange!(int[], const(int))` is true, but
`isForwardRange!(int[], string)` is false.
15 changes: 15 additions & 0 deletions std/range/primitives.d
Original file line number Diff line number Diff line change
Expand Up @@ -1015,12 +1015,27 @@ See_Also:
enum bool isForwardRange(R) = isInputRange!R
&& is(typeof((R r) { return r.save; } (R.init)) == R);

/// ditto
enum bool isForwardRange(R, E) =
.isForwardRange!R && isQualifierConvertible!(ElementType!R, E);

///
@safe unittest
{
static assert(!isForwardRange!(int));
static assert( isForwardRange!(int[]));
static assert( isForwardRange!(inout(int)[]));

static assert( isForwardRange!(int[], const int));
static assert(!isForwardRange!(int[], immutable int));

static assert(!isForwardRange!(const(int)[], int));
static assert( isForwardRange!(const(int)[], const int));
static assert(!isForwardRange!(const(int)[], immutable int));

static assert(!isForwardRange!(immutable(int)[], int));
static assert( isForwardRange!(immutable(int)[], const int));
static assert( isForwardRange!(immutable(int)[], immutable int));
}

@safe unittest
Expand Down