v
v copied to clipboard
Anonymous generic struct shadow variable
V version: OS:
What did you do?
module main
struct Cmd<T> {
mut:
val T
}
fn print<T>(mut t Cmd<T>) {
print(t)
}
fn main() {
mut cmd := Cmd<string>{
val: 'qqq'
}
anon := fn[mut cmd<T>]<T> () {
print(mut cmd)
}
anon()
}
i wanna give access to variable cmd in closure, i dont need to pass it from func arguments. also i tried:
module main
struct Cmd<T> {
mut:
val T
}
fn print<T>(mut t Cmd<T>) {
print(t)
}
fn main() {
mut cmd := Cmd<string>{
val: 'qqq'
}
anon := fn[mut cmd]<T> () {
print(mut cmd)
}
anon()
}
What did you expect to see? Errors
What did you see instead? Using generic struct as shadow variable
With the latest V and new generic syntax:
file.v:16:13: error: cannot assign generic function to a variable
14 | }
15 |
16 | anon := fn[mut cmd][T] () {
| ~~~~~~~~~~~~~~~~~~~
17 | print(mut cmd)
18 | }
I am not sure if it is the right behaviour or not 🤔
according to https://github.com/vlang/v/pull/16507, it "shouldn't" be. can't help but wonder why it was merged to begin with.