From 508f29c10103df36e6b727bbe3eb4c4f3814a45d Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Tue, 26 Oct 2021 10:53:08 +0300 Subject: [PATCH] docs: document the purpose of the `[params]` trailing struct tag --- doc/docs.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/doc/docs.md b/doc/docs.md index f94b83c229..4399384de5 100644 --- a/doc/docs.md +++ b/doc/docs.md @@ -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).