Skip to content

Unify function overloads in std.range#5608

Merged
dlang-bot merged 3 commits intodlang:masterfrom
wilzbach:unify-std-range
Jul 17, 2017
Merged

Unify function overloads in std.range#5608
dlang-bot merged 3 commits intodlang:masterfrom
wilzbach:unify-std-range

Conversation

@wilzbach
Copy link
Contributor

Created a new PR due to CI errors in #5605

@wilzbach wilzbach added the Review:Trivial typos, formatting, comments label Jul 12, 2017
@dlang-bot
Copy link
Contributor

Thanks for your pull request, @wilzbach!

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

@wilzbach
Copy link
Contributor Author

image

Ping me if it happens again, I'll see which SHA is wrong.

Ping :P

}
else
{
static assert(0, "The ranges have no common type.");
Copy link
Member

Choose a reason for hiding this comment

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

This is new behavior - now chooseAmong accepts wrong arguments and errs in the implementation. That is acceptable, but the current prevalent approach is to eliminate unacceptable arguments in the constraint. To do so:

auto chooseAmong(Ranges...)(size_t index, Ranges rs)
if (Ranges.length >= 2
    && allSatisfy!(isInputRange, staticMap!(Unqual, Ranges))
    && !is(CommonType!(staticMap!(ElementType, Ranges)) == void))
{
    ...
}

It's also nicely readable - there have got to be two or more arguments, all of which must be input ranges, which must have some common element type.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hehe this paradigm switch has happened a couple of months ago, see e.g. @quickfur's excellent explananation at #5148 (comment):

And just for the record, the last bit is what I meant in various forum posts that user-facing overloads should have sig constraints that reflect intent rather than implementation: the intent of each is that it would accept anything that either supports foreach or has a range API. So those should be what constitute the sig constraints of each.
There may be, however, some ranges that satisfy these constraints but which aren't handled by the current implementation; these should be caught by the final else clause in the static if inside each overload body, with a helpful error message explaining to the user why it didn't work ("support for range with characteristics XYZ isn't implemented yet", or "cannot call each with range with characteristics XYZ because PQR").

In this case one could argue for both sides, but imho having a concrete message with what went wrong helps the user a lot more than "X didn't match constraints".
I don't feel strongely about this particular case though, so I'm happy to change it as suggested if that's the only thing that blocks this PR. Other Opinions?

Copy link
Member

Choose a reason for hiding this comment

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

I agree 100% with @quickfur's comment, but in this case requiring that there's a common element type between all ranges is part of the intent, not just an implementation detail.
Unless we decide to return a range of Algebraic!(E1, E2, ...) elements, I don't think this will change in the future.

Copy link
Member

Choose a reason for hiding this comment

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

I agree. Given the current implementation, since the return type must be bound at compile-time but the decision of which range to return is made at runtime, we must require returning a common element type.

Please add this to the sig constraint, and we're good to go.

@andralex andralex removed the Review:Trivial typos, formatting, comments label Jul 13, 2017
@andralex
Copy link
Member

Apparently not trivial :)

@andralex
Copy link
Member

ping @wilzbach

@wilzbach
Copy link
Contributor Author

ping @wilzbach

I will get to this soon, just busy with my day job as a student ;-)
However, there's enough enough stuff in the queue that would require your attention.
An easy one is:

#5575

And the full list:

https://github.com/dlang/phobos/pulls?q=is%3Aopen+is%3Apr+label%3A%40andralex

@wilzbach wilzbach force-pushed the unify-std-range branch 2 times, most recently from 29c9edc to f52fd35 Compare July 17, 2017 16:57
@wilzbach wilzbach dismissed andralex’s stale review July 17, 2017 16:58

Finally got around adding the constraint.

@wilzbach
Copy link
Contributor Author

ping @wilzbach

Alrighty, should be finally good to go. Sorry for the hold-up.

@andralex
Copy link
Member

@wilzbach nice comeback :)

Copy link
Member

@andralex andralex left a comment

Choose a reason for hiding this comment

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

Wunderbar

@wilzbach wilzbach closed this Jul 17, 2017
@wilzbach wilzbach reopened this Jul 17, 2017
@dlang-bot dlang-bot merged commit 7359705 into dlang:master Jul 17, 2017
@wilzbach wilzbach deleted the unify-std-range branch December 11, 2017 02:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants