V: function return a function

Created on 11 Dec 2019  路  5Comments  路  Source: vlang/v

fn sqr(n int) int {
    return n * n
}

fn run() (fn (int) int) {
    return sqr
}

fn main()  {
    println(run(5)) // "25"
}

I want to build success but it show an error: expected type _V_MulRet_fn (int) int, but got fn (int) int

Feature Request

Most helpful comment

Never mind, it seems not to be supported yet. Will be fixed this month.

All 5 comments

Try

fn run() fn (int) int {
    return sqr
}

Never mind, it seems not to be supported yet. Will be fixed this month.

@medvednikov And I think it will support closures like Go.

This works now with V 0.1.27 if you change the main function to

fn main()  {
    s := run()
    println(s(5)) // "25"
}

As an improvement to this, would like to suggest
println(run()(5))
should work too. It should give
25
Currently it gives

error.v:11:14: error: unexpected `(`, expecting `,`
    9 | s := run()
   10 | println(s(5))
   11 | println(run()(5))
      |              ^
Was this page helpful?
0 / 5 - 0 ratings

Related issues

arg2das picture arg2das  路  3Comments

jtkirkpatrick picture jtkirkpatrick  路  3Comments

clpo13 picture clpo13  路  3Comments

radare picture radare  路  3Comments

medvednikov picture medvednikov  路  3Comments