v icon indicating copy to clipboard operation
v copied to clipboard

func variable pass in anonymous func error

Open StringNick opened this issue 3 years ago • 1 comments

V version: OS:

What did you do? https://devbits.app/play/GGmC8FzzOxSI

module main

fn test() {
    println('test')
}

fn test2() {
    println('test2')
}

fn main() {
    t1 := test
    t2 := test2
    anon := fn[t1,t2] () {
        t1() 
        t2()
    }
    anon()
}

What did you expect to see? error that not undefined or not error else

What did you see instead? error that error: unknown function:

StringNick avatar Jul 30 '22 19:07 StringNick

Interesting workaround is to wrap the functions in a structure and pass that into the anon function 🫢.

module main
struct Fun{
    t1 fn()
    t2 fn()
}

fn test() {
    println('test')
}

fn test2() {
    println('test2')
}

fn main() {
    funs := Fun{ test, test2 }
    anon := fn[funs] () {
        funs.t1() 
        funs.t2()
    }
    anon()
}

DevrosTheOne avatar Aug 01 '22 04:08 DevrosTheOne