Search⌘ K
AI Features

Value Types vs Reference Types

Explore the distinctions between value types and reference types in D programming. Understand how fixed-length arrays act as value types while slices function as reference types, and learn how these differences influence assignments and memory behavior in your code.

We'll cover the following...

Fixed-length arrays and slices #

D’s arrays and slices show different behavior when it comes to value type versus reference type.
As we have already seen above, slices are reference types. On the other hand, fixed-length arrays are value types. They own their elements, behaving as individual values:

D
import std.stdio;
void main() {
int[3] array1 = [ 10, 20, 30 ];
auto array2 = array1; // array2's elements are different from array1's
array2[0] = 11;
// First array is not affected:
assert(array1[0] == 10);
writeln(array1[0]);
}

array1 is a ...