http://www.rosettacode.org/wiki/Loops/Do-while
Do we really not miss that in Nim?
I think I have used it in Modula and Oberon not very rarely, for instance when reading from keyboard until a valid key is typed. Of course it can be done with "while true -- break". The Wirthian idea was to use Repeat Until instead of a plain loop with break when possible. That is when there is only one exit point. Was that idea wrong?
Swift has it, so it can not be that bad.
There is a Nim template, but I think that use code duplication:
Here's a template without code duplication:
template doWhile(a: expr, b: stmt): stmt =
while true:
b
if not a:
break
var val = 1
doWhile val mod 6 != 0:
val += 1
echo val
You can do the following:
template loop(body: untyped): typed =
while true:
body
template until(cond: typed): typed =
if cond: break
proc main() =
var x = 0
loop:
x += 1
until x mod 6 == 0
echo x
main()
Note that this is a very simple textual substitution, so it's not clear whether it's worth the effort. Observe that until literally is the same as an exit-if construct. While this version until is not limited to occur at the end of the loop, it is also not clear what the point of such a restriction would be. Take a look at Sather, where while! and until! are simply expressed as iterators. And yes, I find it a bit ugly to write while true also, but I'm not sure that having a separate keyword for this kind of loop would be worthwhile.