1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

doc: move Struct update syntax to the Struct section (#16305)

This commit is contained in:
kahsa 2022-11-04 16:01:51 +09:00 committed by GitHub
parent 3061859f25
commit fa7ff09370
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -70,6 +70,8 @@ To do so, run the command `v up`.
* [Default field values](#default-field-values)
* [Required fields](#required-fields)
* [Short struct literal syntax](#short-struct-literal-syntax)
* [Struct update syntax](#struct-update-syntax)
* [Trailing struct literal arguments](#trailing-struct-literal-arguments)
* [Access modifiers](#access-modifiers)
* [Anonymous structs](#anonymous-structs)
* [[noinit] structs](#noinit-structs)
@ -2112,7 +2114,33 @@ println(points) // [Point{x: 10, y: 20}, Point{x: 20, y: 30}, Point{x: 40,y: 50}
Omitting the struct name also works for returning a struct literal or passing one
as a function argument.
#### Trailing struct literal arguments
### Struct update syntax
V makes it easy to return a modified version of an object:
```v
struct User {
name string
age int
is_registered bool
}
fn register(u User) User {
return User{
...u
is_registered: true
}
}
mut user := User{
name: 'abc'
age: 23
}
user = register(user)
println(user)
```
### Trailing struct literal arguments
V doesn't have default function arguments or named arguments, for that trailing struct
literal syntax can be used instead:
@ -2471,32 +2499,6 @@ to reduce allocations and copying.
For this reason V doesn't allow the modification of arguments with primitive types (e.g. integers).
Only more complex types such as arrays and maps may be modified.
#### Struct update syntax
V makes it easy to return a modified version of an object:
```v
struct User {
name string
age int
is_registered bool
}
fn register(u User) User {
return User{
...u
is_registered: true
}
}
mut user := User{
name: 'abc'
age: 23
}
user = register(user)
println(user)
```
### Variable number of arguments
```v