From d52bac8ee9aea7e9342abbe4910d85ca80d2c49f Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 27 Jun 2023 01:31:48 +0300 Subject: [PATCH] doc: document new static type methods --- doc/docs.md | 14 ++++++++++++++ vlib/v/ast/str.v | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/doc/docs.md b/doc/docs.md index aee39d81eb..2b79ef2bb2 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 2a181c64df..49ed3605a3 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}' }