Value Types vs Reference Types

In this lesson we will see how D's arrays and slices behave with value type and reference type. Furthermore, we will perform an experiment to apply == operator to different types.

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:

Press + to interact
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 ...