Skip to content

Commit 838a8f2

Browse files
authored
docs: improve the documentation for struct embedding (#13560)
1 parent 2712e43 commit 838a8f2

File tree

1 file changed

+95
-28
lines changed

1 file changed

+95
-28
lines changed

doc/docs.md

Lines changed: 95 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ For more details and troubleshooting, please visit the [vab GitHub repository](h
8787
* [Match](#match)
8888
* [Defer](#defer)
8989
* [Structs](#structs)
90-
* [Embedded structs](#embedded-structs)
9190
* [Default field values](#default-field-values)
9291
* [Short struct literal syntax](#short-struct-literal-syntax)
9392
* [Access modifiers](#access-modifiers)
9493
* [Methods](#methods)
94+
* [Embedded structs](#embedded-structs)
9595
* [Unions](#unions)
9696

9797
</td><td width=33% valign=top>
@@ -1929,33 +1929,6 @@ println(p.x)
19291929
The type of `p` is `&Point`. It's a [reference](#references) to `Point`.
19301930
References are similar to Go pointers and C++ references.
19311931

1932-
### Embedded structs
1933-
1934-
V doesn't allow subclassing, but it supports embedded structs:
1935-
1936-
```v
1937-
struct Widget {
1938-
mut:
1939-
x int
1940-
y int
1941-
}
1942-
1943-
struct Button {
1944-
Widget
1945-
title string
1946-
}
1947-
1948-
mut button := Button{
1949-
title: 'Click me'
1950-
}
1951-
button.x = 3
1952-
```
1953-
Without embedding we'd have to name the `Widget` field and do:
1954-
1955-
```v oksyntax
1956-
button.widget.x = 3
1957-
```
1958-
19591932
### Default field values
19601933

19611934
```v
@@ -2139,6 +2112,100 @@ In this example, the `can_register` method has a receiver of type `User` named `
21392112
The convention is not to use receiver names like `self` or `this`,
21402113
but a short, preferably one letter long, name.
21412114

2115+
### Embedded structs
2116+
2117+
V support embedded structs .
2118+
2119+
```v
2120+
struct Size {
2121+
mut:
2122+
width int
2123+
height int
2124+
}
2125+
2126+
fn (s &Size) area() int {
2127+
return s.width * s.height
2128+
}
2129+
2130+
struct Button {
2131+
Size
2132+
title string
2133+
}
2134+
```
2135+
2136+
With embedding, the struct `Button` will automatically have get all the fields and methods from
2137+
the struct `Size`, which allows you to do:
2138+
2139+
```v oksyntax
2140+
mut button := Button{
2141+
title: 'Click me'
2142+
height: 2
2143+
}
2144+
2145+
button.width = 3
2146+
assert button.area() == 6
2147+
assert button.Size.area() == 6
2148+
print(button)
2149+
```
2150+
2151+
output :
2152+
```
2153+
Button{
2154+
Size: Size{
2155+
width: 3
2156+
height: 2
2157+
}
2158+
title: 'Click me'
2159+
}
2160+
```
2161+
2162+
Slightly similar to inheritance, a struct will automatically get all the fields and methods
2163+
from its embedded structs.
2164+
2165+
Unlike inheritance however, you cannot type cast between structs and embedded structs
2166+
(the embedding struct can also has its own fields, and it can also embed multiple structs).
2167+
2168+
If you need to access embedded structs directly, use an explicit reference like `button.Size`.
2169+
2170+
Conceptually, embedded structs are similar to [mixin](https://en.wikipedia.org/wiki/Mixin)s
2171+
in OOP, *NOT* base classes.
2172+
2173+
An embedded structs is responsible for implementing a common structure and exposing a few
2174+
functions, just like Lego blocks.
2175+
2176+
It is not recommended to create a bulky base class with a huge number of fields or functions.
2177+
There is no need to import a forest for a banana.
2178+
2179+
> The problem with object-oriented languages is they’ve got all this implicit environment
2180+
> that they carry around with them. You wanted a banana but what you got was a gorilla
2181+
> holding the banana and the entire jungle.
2182+
2183+
—— Joe Armstrong, creator of Erlang progamming language
2184+
2185+
If multiple embedded structs have methods or fields with the same name, or if methods or fields
2186+
with the same name are defined in the struct, you can call functions or assign to variables in
2187+
the embedded struct like `button.Size.area()`.
2188+
2189+
You can also initialize an embedded struct:
2190+
2191+
```v oksyntax
2192+
mut button := Button{
2193+
Size: Size{
2194+
width: 3
2195+
height: 2
2196+
}
2197+
}
2198+
```
2199+
2200+
or assign values:
2201+
2202+
```v oksyntax
2203+
button.Size = Size{
2204+
width: 4
2205+
height: 5
2206+
}
2207+
```
2208+
21422209
## Unions
21432210

21442211
Just like structs, unions support embedding.

0 commit comments

Comments
 (0)