V: suggestion: clea(n|r)er for loop syntax instead of classic C style one

Created on 23 Jul 2020  路  8Comments  路  Source: vlang/v

I feel a for loop with the following syntax:

    for i in [1..30] 2 {
        println(i)
    }
  • looks cleaner

  • is more readable

  • involves less typing

than the archaic C-style for loop.

few more examples:

    // counter decrement
    for i in [44..22] -2 {
        println(i)
    }

    // step omitted
    for i in [1..100] {
        println(i)
    }

    // side-by-side
    for i in [1..30] 2 {
        println(i)
    }

    for i := 1; i < 30; i += 2 {
        println(i)
    }
    // it took me 24 minutes just to type the above loop
Feature Request

Most helpful comment

for i in 0..30 already works, and extended range are already discussed in #4933 (but, to be honest, it's most unlikely that they will be implemented in the language)

All 8 comments

for i in [1..30] 2 {
        println(i)
    }

This doesn't make sense at all.

for i := 1; i < 30; i += 2 {

// it took me 24 minutes just to type the above loop

But in that time you learned a useful V programming construct ;-)

for i in 0..30 already works, and extended range are already discussed in #4933 (but, to be honest, it's most unlikely that they will be implemented in the language)

If you want these types of ranges then just my range library https://github.com/Delta456/range

BTW I've made #5949 to document range for.

for i in [1..30] 2 {
        println(i)
    }

This doesn't make sense at all.

In what way does it not make sense, please elaborate?

@jalelegenda [1..30] in V is array slicing and 2 which is step but it seems to be unreadable.

@jalelegenda
In what way is a C-style for loop unclear? It's clearer to me than your suggestion.

If you're concerned about typing less, you should make use of a text editor that supports creating snippets.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PavelVozenilek picture PavelVozenilek  路  3Comments

lobotony picture lobotony  路  3Comments

aurora picture aurora  路  3Comments

oleg-kachan picture oleg-kachan  路  3Comments

penguindark picture penguindark  路  3Comments