mun
mun copied to clipboard
"while"'s "break" could return value
trafficstars
There is solution to "a break in a while loop cannot return a value" inspired by Python:
- Python's
while(andfor) loop can haveelse:branch that is executed if loop exited with condition and not with break: https://docs.python.org/3.10/reference/compound_stmts.html#the-while-statement This construction is unusual, rare language haselsefor loops (I don't remember one except Python). Therefore almost nobody uses it. - But it could naturally complement "
breakmay return value from loop" feature (which Python has not). It could sound like: "ifwhile's loop body hasbreakwith value, then it has to haveelseblock which results with value of same type".
Thats an interesting idea! Would you be able to create a PR?