V: Support local functions

Created on 21 Mar 2020  Â·  8Comments  Â·  Source: vlang/v

V version: V 0.1.25 c12985d
OS: macOS 10.15

What did you do?

$ cat test_local_function.v
fn main() {
    fn local() {
        println('hello world')
    }
    local()
}
$ v run test_local_function.v

What did you expect to see?

hello world

What did you see instead?

` $ v run test_local_function.v test_local_function.v:2:3: unexpected token: `fn` 1| fn main() { 2| fn local() { ^ 3| println('hello world') 4| }

Feature Request

All 8 comments

oh, no! You can use inline functions instead.

[inline]
fn local() {
    .....
}

inline functions dont have access to the local variables and the scope is global, so it's not a solution. also this inline attribute doesn't work for local functions either

$ v run test_local_function.v
test_local_function.v:2:8: undefined: `inline`
    1| fn main() {
    2|  [inline]
                ^
    3|  fn local() {
    4|      println('hello world')

I asked for that many times and i think it should be implemented.

it's actually almost completely implemented in the new parser

the syntax is

local := fn() {
        println('hello world')
    }

$ v run test_local_function.v
next token = {
test_local_function.v:2:16: syntax error: unexpected ), expecting name
1| fn main() {
2| local := fn() {
^
3| println('hello world')
4| }

On 21 Mar 2020, at 20:23, Alexander Medvednikov notifications@github.com wrote:

it's actually almost completely implemented in the new parser

the syntax is

local := fn() {
println('hello world')
}
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub https://github.com/vlang/v/issues/4087#issuecomment-602090933, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAG75FWGKO75BJZR4MKHKW3RIUH3PANCNFSM4LQ3VBYA.

@radare Like @medvednikov said, it's almost completely implemented so you cannot use it right now (as your error message suggests)

ok thank you :) let me know when i can help/test.

On 25 Mar 2020, at 11:45, Ned Palacios notifications@github.com wrote:

@radare https://github.com/radare Like @medvednikov https://github.com/medvednikov said, it's almost completely implemented so you cannot use it right now (as your error message suggests)

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub https://github.com/vlang/v/issues/4087#issuecomment-603770041, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAG75FSUGECN6TXZP6F4G7DRJHOC3ANCNFSM4LQ3VBYA.

Update: Anonymous functions are in. They don't have local scope, pretty sure that is planned though.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

taojy123 picture taojy123  Â·  3Comments

jtkirkpatrick picture jtkirkpatrick  Â·  3Comments

PavelVozenilek picture PavelVozenilek  Â·  3Comments

ArcDrake picture ArcDrake  Â·  3Comments

vtereshkov picture vtereshkov  Â·  3Comments