v icon indicating copy to clipboard operation
v copied to clipboard

Anonymous generic struct shadow variable

Open StringNick opened this issue 3 years ago • 2 comments

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

StringNick avatar Aug 25 '22 14:08 StringNick

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 🤔

Delta456 avatar Dec 23 '22 12:12 Delta456

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.

kendfss avatar Mar 06 '23 21:03 kendfss