v icon indicating copy to clipboard operation
v copied to clipboard

anonymous function in comptime wrong behaviour

Open mvenditto opened this issue 4 years ago • 1 comments

V version: V 0.2.4 1b691e7.5ab91dd OS: linux, Ubuntu 21.10 (VM)

maybe an edge case, but comptime calls seems to behave wrong if nested inside anon funcs.

What did you do?

struct Foo {}

fn (f Foo) foo() {
	println('foo')
}

fn (f Foo) bar() {
	println('bar')
}

fn main() {
	f := Foo {}
	$for method in Foo.methods {
		x := fn [f]() {
			f.$method()
		}
		x()
	}
}

What did you expect to see?

foo
bar

What did you see instead?

foo
foo

mvenditto avatar Dec 02 '21 17:12 mvenditto

This is because the anon func is being generated using the same name on parser phase.

struct Foo {}

fn (f Foo) foo() {
	println('foo')
}

fn (f Foo) bar() {
	println('bar')
}

fn main() {
	f := Foo {}
	$for method in Foo.methods {
		$if method.name == 'foo' {
			x := fn [method, f]() {
				f.$method()
			}
			x()
		}
		$if method.name == 'bar' {
			x := fn [method, f]() {
				f.$method()
			}
			x()
		}
	}
}

Make it works.

felipensp avatar May 22 '23 12:05 felipensp