diff --git a/doc/docs.md b/doc/docs.md index aee39d81eb6a01..2b79ef2bb2e470 100644 --- a/doc/docs.md +++ b/doc/docs.md @@ -97,6 +97,7 @@ by using any of the following commands in a terminal: * [Trailing struct literal arguments](#trailing-struct-literal-arguments) * [Access modifiers](#access-modifiers) * [Anonymous structs](#anonymous-structs) + * [Static type methods](#static-type-methods) * [[noinit] structs](#noinit-structs) * [Methods](#methods) * [Embedded structs](#embedded-structs) @@ -2431,6 +2432,19 @@ assert book.author.name == 'Samantha Black' assert book.author.age == 24 ``` +### Static type methods + +V now supports static type methods like `User.new()`. These are defined on a struct via +`fn [Type name].[function name] and allow to organize all functions related to a struct: + +```v oksyntax +struct User { } + +fn User.new() User { return User{} } + +user := User.new() +``` + ### `[noinit]` structs V supports `[noinit]` structs, which are structs that cannot be initialised outside the module diff --git a/vlib/v/ast/str.v b/vlib/v/ast/str.v index 2a181c64df8394..49ed3605a33ea3 100644 --- a/vlib/v/ast/str.v +++ b/vlib/v/ast/str.v @@ -382,7 +382,7 @@ pub fn (x Expr) str() string { return '${x.name}(${sargs})${propagate_suffix}' } if x.name.contains('__static__') { - return '${x.mod}.${x.name}(${sargs})${propagate_suffix}1' + return '${x.mod}.${x.name}(${sargs})${propagate_suffix}' } return '${x.mod}.${x.name}(${sargs})${propagate_suffix}' }