Skip to content

Add destructure syntax for array and struct #24071

@viocha

Description

@viocha

Describe the feature

It is hoped to provide a syntax for arrays to extract certain elements into several variables at one time, similar to the result assignment of a multi-return value function. For example, one,two=arr. At the same time, a syntax can be provided to extract certain fields in a struct variable to corresponding variables. For example, {a,b}=obj.

Use Case

fn main(){
	arr:=[1,2,3];
	a,b,_ := arr; // destructure array

	obj:=Foo{
		x: 1,
		y: 2,
	}
	{x,y} := obj; // destructure struct
	println(x,y);
}

struct Foo {
		x int
		y int
}

Proposed Solution

No response

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

Version used

V 0.4.10

Environment details (OS name and version, etc.)

V full version V 0.4.10 54c10ec.54c10ec
OS windows, Microsoft Windows 11 Pro 22631 64-bit
Processor 16 cpus, 64bit, little endian, AMD Ryzen 7 7840H with Radeon 780M Graphics
Memory 6.13GB/27.69GB
V executable D:\Programs\v\v.exe
V last modified time 2025-03-24 06:08:50
V home dir OK, value: D:\Programs\v
VMODULES OK, value: C:\Users\vioch.vmodules
VTMP OK, value: C:\Users\vioch\AppData\Local\Temp\v_0
Current working dir OK, value: C:\Users\vioch
Git version git version 2.45.1.windows.1
V git status Error: fatal: not a git repository (or any of the parent directories): .git
.git/config present false
cc version clang version 17.0.4 (https://github.com/llvm/llvm-project.git 309d55140c46384b6de7a7573206cbeba3f7077f)
gcc version clang version 17.0.4 (https://github.com/llvm/llvm-project.git 309d55140c46384b6de7a7573206cbeba3f7077f)
clang version clang version 17.0.4 (https://github.com/llvm/llvm-project.git 309d55140c46384b6de7a7573206cbeba3f7077f)
msvc version N/A
tcc version tcc version 0.9.27 (x86_64 Windows)
tcc git status N/A
emcc version N/A
glibc version N/A

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

    Feature/Enhancement RequestThis issue is made to request a feature or an enhancement to an existing one.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions