Skip to content

checker: fix generic comparison for conditional assignment (fix #19398) - #19401

Merged
spytheman merged 1 commit into
vlang:masterfrom
yuyi98:fix_array_push_if_expr
Sep 21, 2023
Merged

spytheman merged 1 commit into
vlang:masterfrom
yuyi98:fix_array_push_if_expr

Conversation

@yuyi98

@yuyi98 yuyi98 commented Sep 21, 2023

Copy link
Copy Markdown
Member

This PR fix generic comparison for conditional assignment (fix #19398).

  • Fix generic comparison for conditional assignment.
  • Add test.
fn clip[N](x []N, min N, max N) []N {
	mut result := []N{cap: x.len}
	for value in x {
		result << if value < min {
			min
		} else if value > max {
			max
		} else {
			value
		}
	}
	return result
}

fn main() {
	a := [1, 2, 3, 4, 5]
	dump(a)
	b := clip(a, 2, 4)
	dump(b)
	assert b == [2, 2, 3, 4, 4]
}

PS D:\Test\v\tt1> v run .    
[.\\tt1.v:17] a: [1, 2, 3, 4, 5]
[.\\tt1.v:19] b: [2, 2, 3, 4, 4]

@spytheman spytheman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Excellent. Thank you!

@spytheman
spytheman merged commit baf5d8a into vlang:master Sep 21, 2023
@yuyi98
yuyi98 deleted the fix_array_push_if_expr branch September 21, 2023 13:40
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.

Generic comparison fails for conditional assignment

2 participants