4 ms·
I've actually found that there are a number of situations where I really want: while True: ... if condition: break ... and kinda wish ther
by joatmon-snoo 5y ago
I've actually found that there are a number of situations where I really want:
while True:
...
if condition:
break
...
and kinda wish there was a nicer syntax for this.
- ohazi 5y agoRust has `loop {}` which I thought was weird at first, but also ended up use more than I would have expected.
- kevinmgranger 5y agoOne especially nice part of rust's `loop` is since it's guaranteed to run at least once and to terminate, you can break with a value! let foo = loop { // ... if condition { break value; } // ... }
- dragonwriter 5y agoA Ruby-like “until” or Pascal “repeat...until” (C’s do...while is equivalent but less clear choice of keywords, IMO.) The Pascal form wouldn't really fit Python's indentation-without-closing-marker block syntax preference, but the Ruby version would. If it existed, I’d be tempted to suggest a linter rule that prohibits break inside an until (it should only be used when the main exit condition is the only exit condition.)
- dragonwriter 5y agoToo late to correct, but I can tell I haven't been using Ruby much for a while; Ruby until is just negated while, like C it uses while (or until!) at the end for Pascal-style repeat...until logic.
- tech2 5y agoAnd this is where I really miss Sather: loop while!(condition); ... end; for while, versus loop ... while!(condition); end; for do-while, and loop ... while!(condition); ... end; in place of: while True: ... if not condition: break ... Alas, it never really took off.