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

Remove while loops #2233

Closed
brson opened this issue Apr 17, 2012 · 10 comments
Closed

Remove while loops #2233

brson opened this issue Apr 17, 2012 · 10 comments
Labels
A-codegen Area: Code generation A-grammar Area: The grammar of Rust

Comments

@brson
Copy link
Contributor

brson commented Apr 17, 2012

Likely much more controversial than removing do, but this topic has come up several times. We do use a lot of while loops.

@BrendanEich
Copy link

Can you show some of the while loops in rustc and how they would transform?

I can see losing do, test at the top is safest, but while is pretty useful in C/C++/JS.

/be

@brson
Copy link
Contributor Author

brson commented Apr 17, 2012

OK, I went through the code base and picked out some representative functions. Most of them are simply iterating over a range. I think @marijnh has done this sort of exercise as well.

// Before
fn to_str(v: t) -> str {
    let mut i: uint = 0u;
    let mut rs: str = "";
    while i < v.nbits {
        rs +=
            alt tritv_get(v, i) {
              dont_care { "?" }
              ttrue { "1" }
              tfalse { "0" }
            };
        i += 1u;
    }
    ret rs;
}

// After
fn to_str(v: t) -> str {
    let mut i: uint = 0u;
    let mut rs: str = "";
    uint::range(0, v.nbits) {|i|
        rs +=
            alt tritv_get(v, i) {
              dont_care { "?" }
              ttrue { "1" }
              tfalse { "0" }
            };
        i += 1u;
    }
    ret rs;
}


// Before
while find_pre_post_state_fn(fcx, f_decl, f_body) { }

// After
loop {
    if find_pre_post_state_fn(fcx, f_decl, f_body) { break }
}


// Before
let mut i = 0u;
let mut data = [];
let mut offsets = [];
while i < vec::len(ccx.shape_cx.tag_order) {
    let did = ccx.shape_cx.tag_order[i];
    let variants = ty::enum_variants(ccx.tcx, did);
    let item_tyt = ty::lookup_item_type(ccx.tcx, did);
    let ty_param_count = vec::len(*item_tyt.bounds);
    // etc

// After
let mut i = 0u;
let mut data = [];
let mut offsets = [];
for ccx.shape_cx.tag_order.each {||
    let did = ccx.shape_cx.tag_order[i];
    let variants = ty::enum_variants(ccx.tcx, did);
    let item_tyt = ty::lookup_item_type(ccx.tcx, did);
    let ty_param_count = vec::len(*item_tyt.bounds);
    // etc


// Before
let mut cx = cx;
while option::is_none(cx.block_span) {
    alt cx.parent {
      parent_some(b) { cx = b; }
      parent_none { fail; }
    }
}

// After
let mut cx = cx;
loop {
    if option::is_none(cx.block_span) { break }
    alt cx.parent {
      parent_some(b) { cx = b; }
      parent_none { fail; }
    }
}

I'll note that @dherman has expressed skepticism about this idea and I also think that removing while is possibly an absurd thing to do despite my desire to kill keywords.

@boggle
Copy link
Contributor

boggle commented Apr 17, 2012

I feel uneasy about this: (1) people are very much used to while loops (2) is hard to read (see loop, find if, discover it ends with break) (3) loop { if { break } } contains an unnecessary additional negation (loop _un_less cond) and it is known from PL usability research that the amount of negation in boolean formulas directly translates into a higher chance of getting the control flow wrong.

@bstrie
Copy link
Contributor

bstrie commented Apr 18, 2012

Also keep in mind how the keyword will sound with the labeled break/continue from #2216. For a loop labeled foo, saying skip foo sounds (sort of) like you're telling it to skip the next iteration of foo.

I submit pass, which has precedence as a keyword from Python (though with a different meaning here).

@boggle
Copy link
Contributor

boggle commented Apr 18, 2012

I think 'pass' is good. A four letter word and not used frequently as a variable name.

@bstrie
Copy link
Contributor

bstrie commented Apr 18, 2012

...Sorry, that above comment was intended for 2229.

@graydon
Copy link
Contributor

graydon commented Apr 18, 2012

There is some precedent in Sather for this. It only has one form of loop, infinite, introduced by loop. You break out wherever you want to break out.

My personal preference, though, is to keep while as a convenience form.

@catamorphism
Copy link
Contributor

Is it possible to implement while with a macro that desugars into loop / break, etc.? If yes, then I'm ok with abolishing while. If no, I'd be reluctant to abolish it. Yes, it's true that you can do everything with loop, but it doesn't always seem natural.

@bstrie
Copy link
Contributor

bstrie commented Apr 19, 2012

The familiarity factor of while is more than worth the cost of a keyword, I think.

@brson
Copy link
Contributor Author

brson commented Jun 4, 2012

I think this is not going to happen. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-codegen Area: Code generation A-grammar Area: The grammar of Rust
Projects
None yet
Development

No branches or pull requests

6 participants