Skip to content

docs: os.walk() cannot update mut array passed to its closure - closure needs better explanation about copy vs reference #23166

Open
@jorgeluismireles

Description

@jorgeluismireles

Describe the bug

We pass a mutable array to os.walk closure where the array is appended with content and the array outside the closure is not updated.

Reproduction Steps

import os

fn main() {
	mut files := []string{}
	os.walk('.', fn[mut files](file string) {
		dump('${file}')
		files << file
	})	
	dump('${files}') // <- EMPTY!
}

Expected Behavior

[code.v:6] '$file': ./code
[code.v:6] '$file': ./code.v
[code.v:6] '$file': ./.vmodules/.cache/README.md
[code.v:9] '$files': [ code, code.v, README.md ] // <- three files outside the closure

Current Behavior

[code.v:6] '$file': ./code
[code.v:6] '$file': ./code.v
[code.v:6] '$file': ./.vmodules/.cache/README.md
[code.v:9] '$files': [] // no files outside the closure

Possible Solution

No response

Additional Information/Context

Warning: The bug is reproducible in the playground (see link below). Walking the current folder gives three files. Should be checked if the files are secure in case someone goes beyond just printing the names.

V version

V 0.4.8 9e71e32

Environment details (OS name and version, etc.)

https://play.vlang.io/p/6e3acff8fe

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.

Huly®: V_0.6-21603

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