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

Compiling an Array Slice example does not produce the result mentioned in the documentaion #21487

Open
ookami-001 opened this issue May 10, 2024 · 0 comments
Labels
Unit: Documentation Bugs/feature requests, that are related to the documentations.

Comments

@ookami-001
Copy link

ookami-001 commented May 10, 2024

Describe the issue

Operating System used: Linux Mint 21.2 Cinnamon Edition
Linux Kernel: 5.15.0-105-generic
V Language version installed: V 0.4.5 6a6d8e0
Code Editor used: Visual Studio Code with the V Programming Language plugin

The code example was stored on my machine in a file named main.v. The contents of the file were as follows:

module main

fn main() {

	mut a := [0, 1, 2, 3, 4, 5]
	mut b := a[2..4]
	b[0] = 7 // `b[0]` is referring to `a[2]`
	println(a) // `[0, 1, 7, 3, 4, 5]`
	b << 9
	// `b` has been reallocated and is now independent from `a`
	println(a) // `[0, 1, 7, 3, 4, 5]` - no change
	println(b) // `[7, 3, 9]`

}

After I tried compiling the code above using the following command:

v run .

I got the following output:

src/main.v:6:12: notice: an implicit clone of the slice was done here
    4 | 
    5 |     mut a := [0, 1, 2, 3, 4, 5]
    6 |     mut b := a[2..4]
      |               ~~~~~~
    7 |     b[0] = 7 // `b[0]` is referring to `a[2]`
    8 |     println(a) // `[0, 1, 7, 3, 4, 5]`
Details: src/main.v:6:12: details: To silence this notice, use either an explicit `a[..].clone()`,
or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.
    4 | 
    5 |     mut a := [0, 1, 2, 3, 4, 5]
    6 |     mut b := a[2..4]
      |               ~~~~~~
    7 |     b[0] = 7 // `b[0]` is referring to `a[2]`
    8 |     println(a) // `[0, 1, 7, 3, 4, 5]`
[0, 1, 2, 3, 4, 5]
[0, 1, 2, 3, 4, 5]
[7, 3, 9]

As you can see from the output above, the output of the two println(a) statements was the original unmodified array a:

[0, 1, 2, 3, 4, 5]
[0, 1, 2, 3, 4, 5]

The documentation, however, states that the output should have been the following:

[0, 1, 7, 3, 4, 5]
[0, 1, 7, 3, 4, 5]

Links

https://github.com/vlang/v/blob/master/doc/docs.md#array-slices

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.

@ookami-001 ookami-001 added the Unit: Documentation Bugs/feature requests, that are related to the documentations. label May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Unit: Documentation Bugs/feature requests, that are related to the documentations.
Projects
None yet
Development

No branches or pull requests

1 participant