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
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 caseprintln("${users[999].age}").
thanks
Most helpful comment
Why that, when there already is a working syntax ?