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: fix generics method chaining call (fix #22418) #22447

Merged
merged 1 commit into from
Oct 8, 2024

Conversation

yuyi98
Copy link
Member

@yuyi98 yuyi98 commented Oct 8, 2024

This PR fix generics method chaining call (fix #22418).

  • Fix generics method chaining call.
  • Add test.
struct Seq[T] {
	ar []T
}

type MapFn[T, K] = fn (T) K

fn (s Seq[T]) map[K](map_fn MapFn[T, K]) Seq[K] {
	mut map_ar := []K{cap: s.ar.len}

	for _, i in s.ar {
		map_ar << map_fn[T, K](i)
	}

	return Seq[K]{map_ar}
}

fn main() {
	s := Seq[string]{['one', 'two']}
		.map[int](fn (element string) int {
			match element {
				'one' { return 1 }
				'two' { return 2 }
				else { return -1 }
			}
		})
		.map[int](fn (element int) int {
			return element + 2
		})

	println(s)
}

PS D:\Test\v\tt1> v run .       
Seq[int]{
    ar: [3, 4]
}

Copy link
Member

@spytheman spytheman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work 🙇🏻‍♂️ .

@spytheman spytheman merged commit 4d74d72 into vlang:master Oct 8, 2024
78 checks passed
@yuyi98 yuyi98 deleted the fix_embed_generic_method_call branch October 8, 2024 12:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unknown method or field when using generics
3 participants