Skip to content

Implement range syntax #19858

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

Merged
merged 6 commits into from
Dec 24, 2014
Merged

Implement range syntax #19858

merged 6 commits into from
Dec 24, 2014

Conversation

nrc
Copy link
Member

@nrc nrc commented Dec 15, 2014

Closes #19794

r? @aturon for the first patch
r? @nikomatsakis for the rest

@aturon
Copy link
Member

aturon commented Dec 15, 2014

Yay! Delighted to see this, @nick29581

#[lang="full_range"]
pub struct FullRange;

/// A range which i bounded at both ends.
Copy link
Member

Choose a reason for hiding this comment

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

"is". Might want to mention "half open" in the type docs as well.

@aturon
Copy link
Member

aturon commented Dec 15, 2014

@nick29581 left some comments -- though possibly on a stale commit? In any case, I'm basically fine with landing this as-is in terms of naming and placement of the Countable trait -- I have some thoughts but can move things around during stabilization.

@nrc
Copy link
Member Author

nrc commented Dec 15, 2014

@aturon I like the Step idea, but I prefer that we use an associated type for the result of steps_left, otherwise you get into overflow territory. I think we can use an equality constraint on the associated type <T=uint> to make this work for the size hint (i.e., you only get an Iterator implementation when it is safe to give a size hint, with better trait matching in the future, we could do better and always get an Iterator impl, but only give a size hint if it is safe).

@aturon
Copy link
Member

aturon commented Dec 15, 2014

@nick29581 What about making it return Option<uint>? Its sole purpose is for the size hint, right?

@nrc
Copy link
Member Author

nrc commented Dec 15, 2014

@aturon it is for now, yes (if the trait is meant to be more generally useful, then I guess it should handle more here, but perhaps we should ere on the side of YAGNI here.). Does size_hint need to be fast? I.e., is it worth trying to avoid the branch? If not, then Option seems fine for now.

@nrc nrc assigned nikomatsakis and unassigned aturon Dec 15, 2014
@aturon
Copy link
Member

aturon commented Dec 15, 2014

Usually size_hint is called ahead of fully iterating, so the branch
shouldn't matter much -- but in any case you should be able to use
#[inline].

I also agree that YAGNI seems best here. We recently axed a lot of the
numerics traits to punt general numerics treatments to the Cargoverse --
partly because there are so many ways to do it. Staying focused on the
main use case of iterating over range syntax seems best.

On Mon, Dec 15, 2014 at 3:48 PM, Nick Cameron [email protected]
wrote:

@aturon https://github.com/aturon it is for now, yes (if the trait is
meant to be more generally useful, then I guess it should handle more here,
but perhaps we should ere on the side of YAGNI here.). Does size_hint need
to be fast? I.e., is it worth trying to avoid the branch? If not, then
Option seems fine for now.


Reply to this email directly or view it on GitHub
#19858 (comment).

@sinistersnare
Copy link
Contributor

Is something like the following in this PR? Just a thought I had a few minutes ago.

for i in (0..) {
    println!("infinite loop! i: {}", i);
}

@nrc
Copy link
Member Author

nrc commented Dec 16, 2014

@sinistersnare yes, that will work, you don't even need the brackets around the range

@nikomatsakis
Copy link
Contributor

Exciting.

@nikomatsakis
Copy link
Contributor

So this looks reasonable, though I'm wondering if we can just desugar these expressions in the front-end and save ourselves a lot of weird bugs down the line. This seems like a classic case.

@nikomatsakis
Copy link
Contributor

r+ modulo comments and in particular the question about using a common supertype, though perhaps we can discuss whether to desugar etc on IRC

/// Change self to the previous object.
fn step_back(&mut self);
/// The steps_between two step objects.
/// a should always be less than b, so the result should never be negtive.
Copy link
Contributor

Choose a reason for hiding this comment

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

s/negtive/negative/

bors added a commit that referenced this pull request Dec 24, 2014
@bors bors merged commit e82215d into rust-lang:master Dec 24, 2014
bors added a commit that referenced this pull request Dec 30, 2014
The first six commits are from an earlier PR (#19858) and have already been reviewed. This PR makes an awful hack in the compiler to accommodate slices both natively and in the index a range form. After a snapshot we can hopefully add the new Index impls and then we can remove these awful hacks.

r? @nikomatsakis (or anyone who knows the compiler, really)
alexcrichton added a commit to alexcrichton/rust that referenced this pull request Dec 30, 2014
The first six commits are from an earlier PR (rust-lang#19858) and have already been reviewed. This PR makes an awful hack in the compiler to accommodate slices both natively and in the index a range form. After a snapshot we can hopefully add the new Index impls and then we can remove these awful hacks.

r? @nikomatsakis (or anyone who knows the compiler, really)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add range notation
8 participants