Skip to content

checker: fix generic fn returning generic closure (fix #23035) - #23047

Merged
medvednikov merged 1 commit into
vlang:masterfrom
yuyi98:fix_return_generic_fntype
Dec 2, 2024
Merged

medvednikov merged 1 commit into
vlang:masterfrom
yuyi98:fix_return_generic_fntype

Conversation

@yuyi98

@yuyi98 yuyi98 commented Dec 2, 2024 •

Copy link
Copy Markdown
Member

This PR fix generic fn returning generic closure (fix #23035).

  • Fix generic fn returning generic closure.
  • Add test.
fn vectorize[T](op fn (T) T) fn ([]T) []T {
	return fn [op] [T](values []T) []T {
		mut result := []T{len: values.len}
		for i in 0 .. values.len {
			result[i] = op(values[i])
		}
		return result
	}
}

fn add_one(x f64) f64 {
	return x + 1
}

fn main() {
	vadd := vectorize[f64](add_one)
	v := [1.0, 2, 3, 4]
	println(vadd(v))
	assert vadd(v) == [2.0, 3, 4, 5]
}

PS D:\Test\v\tt1> v run .
[2.0, 3.0, 4.0, 5.0]

Huly®: V_0.6-21487

@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 work.

Comment thread vlib/v/checker/return.v
Comment on lines +270 to +274
// ignore return closure
if node.exprs[expr_idxs[i]] is ast.AnonFn
&& node.exprs[expr_idxs[i]].inherited_vars.len > 0 {
continue
}

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.

will that allow for returning closures, that do not match the signature as well?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, It need to check more.

@medvednikov
medvednikov merged commit 6ac1d08 into vlang:master Dec 2, 2024
@thesombady

Copy link
Copy Markdown
Contributor

Great work! A neat fix!

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.

V generic function return

5 participants