mirror of
https://github.com/vlang/v.git
synced 2025-09-10 16:00:31 -04:00
doc: move Struct update syntax
to the Struct section (#16305)
This commit is contained in:
parent
3061859f25
commit
fa7ff09370
56
doc/docs.md
56
doc/docs.md
@ -70,6 +70,8 @@ To do so, run the command `v up`.
|
|||||||
* [Default field values](#default-field-values)
|
* [Default field values](#default-field-values)
|
||||||
* [Required fields](#required-fields)
|
* [Required fields](#required-fields)
|
||||||
* [Short struct literal syntax](#short-struct-literal-syntax)
|
* [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)
|
* [Access modifiers](#access-modifiers)
|
||||||
* [Anonymous structs](#anonymous-structs)
|
* [Anonymous structs](#anonymous-structs)
|
||||||
* [[noinit] structs](#noinit-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
|
Omitting the struct name also works for returning a struct literal or passing one
|
||||||
as a function argument.
|
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
|
V doesn't have default function arguments or named arguments, for that trailing struct
|
||||||
literal syntax can be used instead:
|
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).
|
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.
|
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
|
### Variable number of arguments
|
||||||
|
|
||||||
```v
|
```v
|
||||||
|
Loading…
x
Reference in New Issue
Block a user