[Mercury Feature Request] Inclusion in the for loop of keyword "Until"

In vb, there is this loop:

Dim uA as Integer
For uA = 0 to 5
    Console.WriteLine(uA+",")
Next uA
' Print:
' 0,1,2,3,4,5

There could also be this form:

Dim uA as Integer
For uA = 0 Until 5
    Console.WriteLine(uA + ",")
Next uA
' Print:
' 0, 1, 2, 3, 4

Notice the difference between “to” and “until”.
“To” is inclusive, “Until” is exclusive.

For uA = 0 Until 5
Next uA

It’s similar to construction in C#:

for(uA = 0; uA < 5; uA++) {}

For me, the construction with Until is more readable than the form below in VB:

Dim limit as Integer
Dim uA as Integer

For uA = 1 to limit - 1
Next uA

With ‘Until’, it becomes less verbose:

For uA = 1 Until limit
Next

On MonkeyX, Monkey2 and CerberusX, what are language based on Basic, this construction exist.

welcome @fabiuz.

I think this is a good suggestion - preventing to have to type Collection.Count - 1 for every for loop (that is easily forgotten, causing runtime crashes).

We’ll keep it on the suggestion list.
.

One concern: I’m not sure if Until clearly expresses the difference in behavior to To? To me, “util” does not imply “less than”. e.g. if I say “I can stay until five”, it does not mean I have to leave before five o’clock.

I’d like a different keyword that makes the distinction more clear.

(l like how Swift uses “for I in 0...5 {” and “for I in 0 ..< 5 {”, as it makes the difference clear immediately. While this does not translate directly into a VB-ish syntax, I’d want something that is similarly obvious.

1 Like

Suggestion: UpTo

For I = 0 UpTo 5 //does 0,1,2,3 and 4

For I = 5 UpTo 0 step -1 //does 5,4,3,2 and 1

1 Like

This “UpTo” keyword is also interesting.
It has the same meaning as the “prefix (upTo :)” method in swift:
https://developer.apple.com/documentation/swift/array/1688336-prefix, that is, it returns an interval from the beginning until not including the final position.

2 Likes