V: Structure array printing

Created on 19 Aug 2020  路  6Comments  路  Source: vlang/v

V version:
V 0.1.29 e69f091

OS:
ubuntu 16.04

What did you do?

struct User{
        name string
        age int
}

fn new_user(i int) User {
        return User{
        name: "liao",
        age: i,
        }
}

fn main(){
        mut users := []User{cap:1000}
        for i in 0.. 1000{
                users << new_user(i)
        }

        println("array len is: $users.len")
        if users.len > 999 {
                // contrary to expectation
                println( "$users[999].age")

                // normal
                //u := users[999]
                //println( " $u.age")
        }
}

What did you expect to see?
array len is: 1000 999
What did you see instead?
The entire object is printed

Bug

Most helpful comment

Why that, when there already is a working syntax ?

All 6 comments

Indexing an array or a string in string interpolation must be done using the ${...} notation, so in you case println("${users[999].age}").

Maybe we could require a backslash after an interpolated symbol:

'$arr[0]' // warning: escape [
'$arr\[0]' // ok

Same goes for $f( -> $f\(.

Why that, when there already is a working syntax ?

@Gladear Interpolated strings like '$f(...)' and '$a[...]' are confusing to read, so why not require a backslash to make it clear that they are not calling a function or indexing an array.

Edit: Just realized '$f(...)' does work. That makes it strange that '$a[...]' doesn't work.

Indexing an array or a string in string interpolation must be done using the ${...} notation

Why?

Indexing an array or a string in string interpolation must be done using the ${...} notation, so in you case println("${users[999].age}").

thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aurora picture aurora  路  3Comments

oleg-kachan picture oleg-kachan  路  3Comments

lobotony picture lobotony  路  3Comments

medvednikov picture medvednikov  路  3Comments

choleraehyq picture choleraehyq  路  3Comments