Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closures fail when capturing parameters that are functions #13032

Closed
svelterust opened this issue Jan 4, 2022 · 1 comment
Closed

Closures fail when capturing parameters that are functions #13032

svelterust opened this issue Jan 4, 2022 · 1 comment
Labels
Bug This tag is applied to issues which reports bugs.

Comments

@svelterust
Copy link

V version: V 0.2.4 4d4398f
OS: linux, "Arch Linux"

What did you do?
Try capturing functions from function parameter.

What did you expect to see?
No C error.

What did you see instead?
Didn't work. This causes a C error. There is a temporary workaround in the meantime.

For instance, if we have the following:

pub fn minimal(cond fn (int) bool) fn (int) bool {
	return fn [cond] (input int) bool {
		return cond(input)
	}
}

We get this:

~/source/vlang/parser $ v -cg .
/tmp/v_1000/parser.9950605039529488339.tmp.c:1914: warning: implicit declaration of function 'cond'
tcc: error: undefined symbol 'cond'

However, if we declare the parameter again inside the function body, it works:

pub fn minimal(cond fn (int) bool) fn (int) bool {
	functions := [cond]
	return fn [functions] (input int) bool {
		cond := functions[0]
		return cond(input)
	}
}
@svelterust svelterust added the Bug This tag is applied to issues which reports bugs. label Jan 4, 2022
@spytheman
Copy link
Member

The first minimal example now compiles.

pub fn minimal(cond fn (int) bool) fn (int) bool {
	return fn [cond] (input int) bool {
		return cond(input)
	}
}

fn main() {
	x := minimal(fn (i int) bool {
		if i < 10 {
			return false
		}
		return true
	})
	//
	dump(x(-5))
	dump(x(10))
	dump(x(20))
}

produces:

[zz.v:10] main.x(-5): false
[zz.v:11] main.x(10): true
[zz.v:12] main.x(20): true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs.
Projects
None yet
Development

No branches or pull requests

2 participants