v icon indicating copy to clipboard operation
v copied to clipboard

Struct generic fn as variable field

Open StringNick opened this issue 3 years ago • 1 comments

V version: OS:

What did you do?

module main

struct Cmd<T> {
    mut:
    	val T
}

struct Test {
    mut:
    	f fn<T> (mut Cmd<T>) 
}
fn print<T>(mut t Cmd<T>) {
    print(t)
}

fn main() {
    mut tes := Test{
        f: print
    }
    
    mut cmd := Cmd<string>{
        val: 'qqq'
    }
    t.f()
}

What did you expect to see? No error

What did you see instead?

main.v:10:10: error: unexpected token `<`, expecting `(`
    8 | struct Test {
    9 |     mut:
   10 |         f fn<T> (mut Cmd<T>) 
      |             ^
   11 | }
   12 | fn print<T>(mut t Cmd<T>) {

StringNick avatar Aug 25 '22 11:08 StringNick

syntax error:

struct Test {
    mut:
    	f fn<T> (mut Cmd<T>) 
}

generic fn cannot be fn variable.

module main

struct Cmd<T> {
    mut:
    	val T
}

struct Test<T> {
    mut:
    	f fn (mut t Cmd<T>)
}

fn print_cmd(mut t Cmd<string>) {
    print(t)
}

fn main() {
    mut tes := Test<string>{
        f: print_cmd
    }

    mut cmd := Cmd<string>{
        val: 'qqq'
    }
    tes.f(mut cmd)
}

PS D:\Test\v\tt1> v run .
Cmd<string>{
    val: 'qqq'
}

yuyi98 avatar Aug 25 '22 12:08 yuyi98