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

doc: clarify the short struct syntax

This commit is contained in:
Alexander Medvednikov 2020-05-17 16:15:02 +02:00 committed by GitHub
parent 89d7d937ae
commit a66eebc651
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -36,7 +36,7 @@ you can do in V.
<tr> <tr>
<td><a href='#match'>Match</a></td> <td><a href='#match'>Match</a></td>
<td><a href='#structs'>Structs</a></td> <td><a href='#structs'>Structs</a></td>
<td><a href='#short-struct-initialiation-syntax'>Short struct init syntax</a></td> <td><a href='#short-struct-initialization-syntax'>Short struct init syntax</a></td>
<td><a href='#access-modifiers'>Access modifiers</a></td> <td><a href='#access-modifiers'>Access modifiers</a></td>
<td><a href='#methods'>Methods</a></td> <td><a href='#methods'>Methods</a></td>
<td><a href='#pure-functions-by-default'>Pure functions by default</a></td> <td><a href='#pure-functions-by-default'>Pure functions by default</a></td>
@ -683,6 +683,7 @@ struct ButtonConfig {
width int = 70 width int = 70
height int = 20 height int = 20
} }
fn new_button(c ButtonConfig) &Button { fn new_button(c ButtonConfig) &Button {
return &Button{ return &Button{
width: c.width width: c.width
@ -694,6 +695,20 @@ 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 20, the default value
``` ```
As you can see, we can use
```
new_button(text:'Click me', width:100)
```
instead of
```
new_button(ButtonConfig{text:'Click me', width:100})
```
This only works with functions that have a single struct argument.
## Access modifiers ## Access modifiers
Struct fields are private and immutable by default (making structs immutable as well). Struct fields are private and immutable by default (making structs immutable as well).