Nim: Add since() and until() in loops!

Created on 30 Aug 2018  路  5Comments  路  Source: nim-lang/Nim

There are not many languages which implements since() and until() in loops.
I would like to see them in Nim in the future updates.

since() block is executed when a condition is met like if() but it keeps executing it until the loop ends.
until() block is executed until a condition is met and afterwards it stops executing it in a loop.

Feature

Most helpful comment

Seems easy enough to do with a template/macro and doesn't require a language change.

All 5 comments

please let me know if it will be added or not in the upcoming versions.

Seems easy enough to do with a template/macro and doesn't require a language change.

Nim has already while loops.
I think until loop won't be added and this issue can be closed.

template since(cond, body): untyped =
  if cond:
    while true:
      body

template until(cond, body): untyped =
  while not cond:
    body

since 3 > 5:
  echo "never executed"

since 5 > 3:
  echo "infinite loop if we don't break"
  break

var i = 10
until i == 0:
  echo "i: ", i
  dec i
echo "i should be 0 and is: ", i

I assume this is what you want, but where would it go? The sugar module?

I assume this is what you want, but where would it go? The sugar module?

No, sugar is not a dumping ground for syntax games. Nor is the rest of Nim's stdlib. I consider this closed with your fine examples. The OP is free to use these templates, it doesn't require stdlib support.

Was this page helpful?
0 / 5 - 0 ratings