@@ -87,11 +87,11 @@ For more details and troubleshooting, please visit the [vab GitHub repository](h
87
87
* [ Match] ( #match )
88
88
* [ Defer] ( #defer )
89
89
* [ Structs] ( #structs )
90
- * [ Embedded structs] ( #embedded-structs )
91
90
* [ Default field values] ( #default-field-values )
92
91
* [ Short struct literal syntax] ( #short-struct-literal-syntax )
93
92
* [ Access modifiers] ( #access-modifiers )
94
93
* [ Methods] ( #methods )
94
+ * [ Embedded structs] ( #embedded-structs )
95
95
* [ Unions] ( #unions )
96
96
97
97
</td ><td width =33% valign =top >
@@ -1929,33 +1929,6 @@ println(p.x)
1929
1929
The type of ` p ` is ` &Point ` . It's a [ reference] ( #references ) to ` Point ` .
1930
1930
References are similar to Go pointers and C++ references.
1931
1931
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
-
1959
1932
### Default field values
1960
1933
1961
1934
``` v
@@ -2139,6 +2112,100 @@ In this example, the `can_register` method has a receiver of type `User` named `
2139
2112
The convention is not to use receiver names like ` self ` or ` this ` ,
2140
2113
but a short, preferably one letter long, name.
2141
2114
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
+
2142
2209
## Unions
2143
2210
2144
2211
Just like structs, unions support embedding.
0 commit comments