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

spawn a function with mut array not allowed #21280

Closed
Eliyaan opened this issue Apr 14, 2024 · 0 comments · Fixed by #21283
Closed

spawn a function with mut array not allowed #21280

Eliyaan opened this issue Apr 14, 2024 · 0 comments · Fixed by #21283
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Unit: Checker Bugs/feature requests, that are related to the type checker.

Comments

@Eliyaan
Copy link
Contributor

Eliyaan commented Apr 14, 2024

Describe the bug

When trying to pass a mutable array to a spawn function call, the compiler tells to use references and when trying to use a reference of the array the compiler tells that arrays are already references so we need to use mut instead.

Reproduction Steps

These are the two cases:

fn mutable_array_arg(mut a []int) {
  println(a)
}

fn main() {
  mut a := []int{}
  spawn mutable_array_arg(mut a)
}
fn ref_array_arg(a &[]int) {
  println(a)
}

fn main() {
  mut a := []int{}
  spawn ref_array_arg(&a)
}

Expected Behavior

To be able to pass the mut array as an argument to the function. (or the compiler telling me how to do it)

Current Behavior

t.v:7:31: error: function in `spawn` statement cannot contain mutable non-reference arguments
    5 | fn main() {
    6 |   mut a := []int{}
    7 |   spawn mutable_array_arg(mut a)
      |                               ^
    8 | 
    9 | }
t.v:1:25: error: V arrays are already references behind the scenes,
there is no need to use a reference to an array (e.g. use `[]string` instead of `&[]string`).
If you need to modify an array in a function, use a mutable argument instead: `fn foo(mut s []string) {}`.
    1 | fn ref_array_arg(mut a &[]int) {
      |                         ^
    2 |   println(a)
    3 | }

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.5 149cbba

Environment details (OS name and version, etc.)

V full version: V 0.4.5 294f7e4.149cbba
OS: linux, Linux version 6.6.13-200.fc39.x86_64 (mockbuild@72f11b2996ed4699b0f705186172808f) (gcc (GCC) 13.2.1 20231205 (Red Hat 13.2.1-6), GNU ld version 2.40-13.fc39) #1 SMP PREEMPT_DYNAMIC Sat Jan 20 18:03:28 UTC 2024
Processor: 4 cpus, 64bit, little endian, Intel(R) Pentium(R) Gold 7505 @ 2.00GHz

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.

@Eliyaan Eliyaan added the Bug This tag is applied to issues which reports bugs. label Apr 14, 2024
@felipensp felipensp self-assigned this Apr 14, 2024
@felipensp felipensp added the Unit: Checker Bugs/feature requests, that are related to the type checker. label Apr 14, 2024
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. Unit: Checker Bugs/feature requests, that are related to the type checker.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants