Skip to content

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

@ookami-001

Description

@ookami-001

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Unit: DocumentationBugs/feature requests, that are related to the documentations.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions