Skip to content

Commit

Permalink
AK: Make Vector<T>::{first,last}_matching() return Optional<T&>
Browse files Browse the repository at this point in the history
These functions are _very_ misleading, as `first()` and `last()` return
references, but `{first,last}_matching()` return copies of the values.
This commit makes it so that they now return Optional<T&>, eliminating
the copy and the confusion.
  • Loading branch information
alimpfard authored and awesomekling committed Apr 4, 2022
1 parent 33e27c5 commit 188207e
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions AK/Vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ requires(!IsRvalueReference<T>) class Vector {
VisibleType& last() { return at(size() - 1); }

template<typename TUnaryPredicate>
Optional<VisibleType> first_matching(TUnaryPredicate predicate) requires(!contains_reference)
Optional<VisibleType&> first_matching(TUnaryPredicate predicate) requires(!contains_reference)
{
for (size_t i = 0; i < size(); ++i) {
if (predicate(at(i))) {
Expand All @@ -174,7 +174,7 @@ requires(!IsRvalueReference<T>) class Vector {
}

template<typename TUnaryPredicate>
Optional<VisibleType> last_matching(TUnaryPredicate predicate) requires(!contains_reference)
Optional<VisibleType&> last_matching(TUnaryPredicate predicate) requires(!contains_reference)
{
for (ssize_t i = size() - 1; i >= 0; --i) {
if (predicate(at(i))) {
Expand Down

0 comments on commit 188207e

Please sign in to comment.