Skip to content
Merged
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
60 changes: 60 additions & 0 deletions std/algorithm/iteration.d
Original file line number Diff line number Diff line change
Expand Up @@ -2415,6 +2415,21 @@ if (isInputRange!RoR && isInputRange!(ElementType!RoR))
return copy;
}
}

static if (hasAssignableElements!(ElementType!RoR))
{
@property void front(ElementType!(ElementType!RoR) element)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

auto ref perhaps?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't auto ref args need to be templated? It didn't compile when I tried it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, they do.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe an overload in hasLvalueElements with a ref parameter would be appropriate?

{
assert(!empty, "Attempting to assign to front of an empty joiner.");
_current.front = element;
}

@property void front(ref ElementType!(ElementType!RoR) element)
{
assert(!empty, "Attempting to assign to front of an empty joiner.");
_current.front = element;
}
}
}
return Result(r);
}
Expand Down Expand Up @@ -2574,6 +2589,51 @@ if (isInputRange!RoR && isInputRange!(ElementType!RoR))
assert(str == "abcd");
}

@safe unittest
{
import std.range : repeat;

class AssignableRange
{
@safe:
int element;
@property int front()
{
return element;
}

enum empty = false;

void popFront()
{
}

@property void front(int newValue)
{
element = newValue;
}
}

static assert(isInputRange!AssignableRange);
static assert(is(ElementType!AssignableRange == int));
static assert(hasAssignableElements!AssignableRange);
static assert(!hasLvalueElements!AssignableRange);

auto range = new AssignableRange();
assert(range.element == 0);

auto joined = joiner(repeat(range));
joined.front = 5;
assert(range.element == 5);
assert(joined.front == 5);

joined.popFront;
int byRef = 7;
joined.front = byRef;
assert(range.element == byRef);
assert(joined.front == byRef);
}

/++
Implements the homonym function (also known as $(D accumulate), $(D
compress), $(D inject), or $(D foldl)) present in various programming
Expand Down