You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/content/docs/guide/memory-management.mdx
+13-40Lines changed: 13 additions & 40 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -129,18 +129,18 @@ This happens automatically - **you don't need to call any special methods** when
129
129
130
130
### Nested Structs
131
131
132
-
When a struct contains members with postblit constructors, D automatically calls postblit on each member:
132
+
When a struct contains members with postblit constructors, D automatically calls postblit on each member - **no explicit postblit is needed** in the containing struct:
133
133
134
134
```d
135
135
struct ValueEvaluation {
136
136
HeapString strValue; // Has postblit
137
137
HeapString niceValue; // Has postblit
138
+
HeapString fileName; // Has postblit
139
+
HeapString prependText; // Has postblit
138
140
139
-
// D automatically calls strValue.this(this) and niceValue.this(this)
141
+
// No explicit postblit needed!
142
+
// D automatically calls the postblit for each HeapString field
rhs._payload.heap = null; // Prevent rhs destructor from freeing
257
226
rhs.setHeap(false);
258
227
} else {
259
-
_payload.small = rhs._payload.small;
228
+
_payload.small = rhs._payload.small; // Copy SBO data
260
229
}
261
230
}
262
231
```
263
232
233
+
When called with an lvalue, D invokes the postblit constructor on `rhs` first, incrementing the ref count. The assignment then takes ownership of the copied reference. This unified approach keeps the code simpler while handling all cases correctly.
234
+
264
235
## Accessing HeapString Content
265
236
266
237
Use the slice operator `[]` to get a `const(char)[]` from a HeapString:
0 commit comments