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

checker: fix generic fn with nested generic fn call (fix #18285) #18314

Merged
merged 2 commits into from
Jun 2, 2023

Conversation

yuyi98
Copy link
Member

@yuyi98 yuyi98 commented May 30, 2023

This PR fix generic fn with nested generic fn call (fix #18285).

  • Fix generic fn with nested generic fn call.
  • Add test.
struct Test {}

fn unmarshal[T]() ! {
	get_number[int]()!
}

fn get_number[T]() !T {
	return T(42)
}

fn main() {
	unmarshal[Test]()!
	assert true
}

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

@prantlf
Copy link

prantlf commented Jun 2, 2023

I can confirm that the fix works well.

A function call with an explicit generic type:

fn unmarshal_number[T](a Any) !T { ... }

fn unmarshal_struct[T](mut dst T, anysrc Any) ! {
  src := anysrc.object()!
  $for field in T.fields {
    anyval := src[field.name]!
    $if field.typ is int {
      dst.$(field.name) = unmarshal_number[int](anyval)!
    }
    ...

A function call with an inferred generic type:

fn convert_number[T](float f64, num T) !T { ... }

fn unmarshal_struct[T](mut dst T, anysrc Any) ! {
  src := anysrc.object()!
  $for field in T.fields {
    anyval := src[field.name]!
    $if field.typ is int {
      num := anyval.number()!
      dst.$(field.name) = convert_number(num, int(num))!
    }
    ...

I'm looking forward to seeing this fix merged :-)

@medvednikov medvednikov merged commit 5e12d34 into vlang:master Jun 2, 2023
49 checks passed
@prantlf
Copy link

prantlf commented Jun 2, 2023

Damn' fast you were :-) Thank you!

@yuyi98 yuyi98 deleted the fix_nested_generic_fn branch June 4, 2023 06:49
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.

Calling generic function from a generic funtion for a different generic type does not compile
4 participants