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

V doesn't figure out array type in match, but it should #22828

Closed
medvednikov opened this issue Nov 10, 2024 · 2 comments · Fixed by #22832
Closed

V doesn't figure out array type in match, but it should #22828

medvednikov opened this issue Nov 10, 2024 · 2 comments · Fixed by #22832
Labels
Bug This tag is applied to issues which reports bugs.

Comments

@medvednikov
Copy link
Member

medvednikov commented Nov 10, 2024

Describe the bug

arr1 := ['Peter', 'Bob']
arr2 := ['Sam', 'Mike']
typ := 1
names := match typ {
	1 {
		arr1
	}
	2 {
		arr2
	}
	else {
		[]
	}
}
println(names)

Should compile, instead getting an error:

code.v:12:3: error: array_init: no type specified (maybe: `[]Type{}` instead of `[]`)
   10 |     }
   11 |     else {
   12 |         []
      |         ~~
   13 |     }
   14 | }
code.v:12:3: error: return type mismatch, it should be `[]string`
   10 |     }
   11 |     else {
   12 |         []
      |         ~~
   13 |     }
   14 | }

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

@medvednikov medvednikov added the Bug This tag is applied to issues which reports bugs. label Nov 10, 2024
@jorgeluismireles
Copy link

I think the next case is related to what is reported here:

fn count_str(array []string) int {
	return array.len
}
fn count_int(array []int) int {
	return array.len
}
fn count[T](array []T) int {
	return array.len
}

assert count_str(['one','two']) == 2
assert count_str([]string{}) == 0
assert count_str([]) == 0 // WORKS

assert count_int([1,2]) == 2
assert count_int([]int{}) == 0
assert count_int([]) == 0 // WORKS

assert count[f64]([1.0,2.0]) == 2
assert count[f64]([]f64{}) == 0
assert count[f64]([]) == 0 // error: cannot use `[]T` as `[]f64` in argument 1 to `count`

All assert work but the last one. Empty array [] type is figured out in calling functions with defined types already but it doesn't for generics.

@medvednikov
Copy link
Member Author

@jorgeluismireles this an issue with generics. Please create a separate issue.

kahgoh added a commit to kahgoh/exercism-vlang that referenced this issue Jan 16, 2025
This is to fix the following error in the CI build:

  error: array_init: no type specified (maybe: `[]Type{}` instead of `[]`)

This probably started happening with Vlang 0.4.9, which has changes
to empty arrays (see vlang/v#22828 and
https://github.com/vlang/v/blob/master/CHANGELOG.md).
keiravillekode pushed a commit to exercism/vlang that referenced this issue Jan 17, 2025
* Update how empty array is initialized

This is to fix the following error in the CI build:

  error: array_init: no type specified (maybe: `[]Type{}` instead of `[]`)

This probably started happening with Vlang 0.4.9, which has changes
to empty arrays (see vlang/v#22828 and
https://github.com/vlang/v/blob/master/CHANGELOG.md).

* Update utf8.get_uchar to .get_rune

This is to fix the following warning:

  warning: function `encoding.utf8.get_uchar` has been deprecated since 2024-11-17, it will be an error after 2025-05-16; use `.get_rune(s string, index int)` instead

[no important files changed]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants