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

doc: struct literals (#5726)

This commit is contained in:
Nick Treleaven 2020-07-08 15:02:35 +01:00 committed by GitHub
parent 64e9bdc213
commit f834276803
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 15 deletions

View File

@ -36,7 +36,7 @@ you can do in V.
* [Match](#match)
* [Defer](#defer)
* [Structs](#structs)
* [Short struct init syntax](#short-struct-initialization-syntax)
* [Trailing struct literal syntax](#short-struct-initialization-syntax)
* [Access modifiers](#access-modifiers)
* [Methods](#methods)
* [println](#println)
@ -715,21 +715,30 @@ struct Point {
y int
}
p := Point{
mut p := Point{
x: 10
y: 20
}
println(p.x) // Struct fields are accessed using a dot
// Alternative literal syntax for structs with 3 fields or fewer
p = Point{10, 20}
assert p.x == 10
// you can omit the struct name when it's already known
p = {x: 30, y: 4}
assert p.y == 4
```
Omitting the struct name also works for function arguments.
<p>&nbsp;</p>
Structs are allocated on the stack. To allocate a struct on the heap
and get a reference to it, use the `&` prefix:
```v
// Alternative initialization syntax for structs with 3 fields or fewer
p := &Point{10, 10}
// References have the same syntax for accessing fields
println(p.x)
@ -772,9 +781,10 @@ All struct fields are zeroed by default during the creation of the struct. Array
It's also possible to define custom default values.
### Short struct initialization syntax
<a id='short-struct-initialization-syntax' />
### Trailing struct literal syntax
There are no default function argument values or named arguments, for that the short struct initialization syntax can be used instead:
There are no default function arguments or named arguments, for that trailing struct literal syntax can be used instead:
```v
struct ButtonConfig {
@ -792,7 +802,9 @@ fn new_button(c ButtonConfig) &Button {
}
}
button := new_button(text:'Click me', width:100) // the height is unset, so it's 20, the default value
button := new_button(text:'Click me', width:100)
// the height is unset, so it's the default value
assert button.height == 20
```
As you can see, we can use
@ -807,7 +819,7 @@ instead of
new_button(ButtonConfig{text:'Click me', width:100})
```
This only works with functions that have a single struct argument.
This only works for functions that have a struct for the last argument.
### Access modifiers

View File

@ -235,22 +235,33 @@ struct Config {
def int = 10
}
fn foo_config(c Config) {
fn foo_config(def int, c Config) {
assert c.def == def
}
fn bar_config(c Config, def int) {
assert c.def == def
}
fn foo2(u User) {
}
fn foo_user(u User) {}
fn test_config() {
foo_config({
fn test_struct_literal_args() {
foo_config(20, {
n: 10
def: 20
})
foo_config({})
foo2({
foo_config(10, {})
foo_config(10, n: 40)
foo_config(40, n: 30, def: 40)
bar_config({}, 10)
bar_config({def:4}, 4)
foo_user({
name: 'Peter'
})
foo2(name: 'Peter')
foo_user(name: 'Peter')
foo_user(age: 7)
foo_user(name: 'Stew', age: 50)
}
struct City {