docs: document the purpose of the `[params]` trailing struct tag

pull/12301/head
Delyan Angelov 2021-10-26 10:53:08 +03:00
parent 655b5c563a
commit 508f29c101
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
1 changed files with 8 additions and 0 deletions

View File

@ -1853,6 +1853,7 @@ V doesn't have default function arguments or named arguments, for that trailing
literal syntax can be used instead:
```v
[params]
struct ButtonConfig {
text string
is_disabled bool
@ -1887,6 +1888,13 @@ new_button(ButtonConfig{text:'Click me', width:100})
This only works for functions that take a struct for the last argument.
NB: the `[params]` tag is used to tell V, that the trailing struct parameter
can be ommited *entirely*, so that you can write `button := new_button()`.
Without it, you have to specify *at least* one of the field names, even if it
has its default value, otherwise the compiler will produce this error message,
when you call the function with no parameters:
`error: expected 1 arguments, but got 0`.
### Access modifiers
Struct fields are private and immutable by default (making structs immutable as well).