doc: improve embedded struct section (#13574)
parent
114a341f5f
commit
9662b79662
27
doc/docs.md
27
doc/docs.md
|
@ -2159,10 +2159,7 @@ Button{
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Slightly similar to inheritance, a struct will automatically get all the fields and methods
|
Unlike inheritance, you cannot type cast between structs and embedded structs
|
||||||
from its embedded structs.
|
|
||||||
|
|
||||||
Unlike inheritance however, 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).
|
(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`.
|
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
|
Conceptually, embedded structs are similar to [mixin](https://en.wikipedia.org/wiki/Mixin)s
|
||||||
in OOP, *NOT* base classes.
|
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:
|
You can also initialize an embedded struct:
|
||||||
|
|
||||||
```v oksyntax
|
```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
|
## Unions
|
||||||
|
|
||||||
Just like structs, unions support embedding.
|
Just like structs, unions support embedding.
|
||||||
|
|
Loading…
Reference in New Issue