From 9662b796626e1b40cc8fef584830c934e5ab042e Mon Sep 17 00:00:00 2001 From: kahsa Date: Wed, 23 Feb 2022 22:54:00 +0900 Subject: [PATCH] doc: improve embedded struct section (#13574) --- doc/docs.md | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/doc/docs.md b/doc/docs.md index 35267747fd..0ba30d849c 100644 --- a/doc/docs.md +++ b/doc/docs.md @@ -2159,10 +2159,7 @@ Button{ } ``` -Slightly similar to inheritance, a struct will automatically get all the fields and methods -from its embedded structs. - -Unlike inheritance however, you cannot type cast between structs and embedded structs +Unlike inheritance, you cannot type cast between structs and embedded structs (the embedding struct can also has its own fields, and it can also embed multiple structs). If you need to access embedded structs directly, use an explicit reference like `button.Size`. @@ -2170,22 +2167,6 @@ If you need to access embedded structs directly, use an explicit reference like Conceptually, embedded structs are similar to [mixin](https://en.wikipedia.org/wiki/Mixin)s in OOP, *NOT* base classes. -An embedded structs is responsible for implementing a common structure and exposing a few -functions, just like Lego blocks. - -It is not recommended to create a bulky base class with a huge number of fields or functions. -There is no need to import a forest for a banana. - -> The problem with object-oriented languages is they’ve got all this implicit environment -> that they carry around with them. You wanted a banana but what you got was a gorilla -> holding the banana and the entire jungle. - -—— Joe Armstrong, creator of Erlang progamming language - -If multiple embedded structs have methods or fields with the same name, or if methods or fields -with the same name are defined in the struct, you can call functions or assign to variables in -the embedded struct like `button.Size.area()`. - You can also initialize an embedded struct: ```v oksyntax @@ -2206,6 +2187,12 @@ button.Size = Size{ } ``` +If multiple embedded structs have methods or fields with the same name, or if methods or fields +with the same name are defined in the struct, you can call methods or assign to variables in +the embedded struct like `button.Size.area()`. +When you do not specify the embedded struct name, the method of the outermost struct will be +targeted. + ## Unions Just like structs, unions support embedding.