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

parser, checker, cgen: implement generics anon fn (fix #15120) #15529

Merged
merged 1 commit into from
Aug 25, 2022

Conversation

yuyi98
Copy link
Member

@yuyi98 yuyi98 commented Aug 25, 2022

This PR implement generics anon fn (fix #15120, fix #13253).

  • Implement generics anon fn.
  • Add test.
fn setter<T>(mut m map[T]int) fn (T, int) {
	return fn [mut m] <T>(x T, k int) {
		m[x] = k
	}
}

fn main() {
	mut m := {
		f32(0.1): 1
	}

	f := setter(mut m)
	f(0.2, 2)

	println(m)
	assert m == {
		f32(0.1): 1
		0.2:      2
	}
}

PS D:\Test\v\tt1> v run .
{0.1: 1, 0.2: 2}
fn closure<T>(input T) fn () T {
	return fn [input] <T>() T {
		return input
	}
}

fn main() {
	f := closure(123)
	println(f())
}

PS D:\Test\v\tt1> v run .
123

@medvednikov medvednikov merged commit 40c0a8c into vlang:master Aug 25, 2022
@yuyi98 yuyi98 deleted the implement_generic_anon_fn branch August 25, 2022 11:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants