D (The Programming Language)/d2/Strings and Dynamic Arrays

=Lesson 8: Strings and Dynamic Arrays= In this chapter, you will learn about strings in depth. In the meantime, you will also learn about another type: the dynamic array.

Dynamic Arrays versus Static Arrays
In the previous lesson, you learned about static arrays. Dynamic arrays are different from those in that they do not have a fixed length. Essentially, a dynamic array is a structure with this info: You create a dynamic array like this: You can create a dynamic array initiated with a specific length: You will learn more about the  keyword later. The syntax for filling an array with a single value is the same for both static and dynamic arrays: Dynamic arrays are indexed in the same fashion as static arrays. The syntax for accessing an element at a certain index is the same: However, since dynamic arrays don't have a length known at compile-time, the compiler can't check if the index that you are accessing is really within that length. Code like this would compile but would also cause a runtime Range Violation error:
 * 1) A pointer to the first element
 * 2) The length of the entire array

Manipulation of Dynamic Arrays
Dynamic arrays can be combined with other dynamic arrays or even with static arrays using the  operator. Appending a new element to the array uses the same operator.

You shouldn't assign a dynamic array to a static array unless if you are certain that   is the equal to. Otherwise, a runtime error will occur. You can always assign a static array to a dynamic array variable, though, since the dynamic array will automatically resize if the static array's length is too big.

Passing Dynamic Arrays to Functions
Dynamic Arrays are passed by value to functions. That means, when you pass a dynamic array to a function, the structure that contains the pointer to the first element and the length is copied and passed. You learned in the last chapter that you can cause things to be passed by reference instead of by value by adding the  modifier.

Array and Other Properties
These properties apply to both static and dynamic arrays: There are also other properties which are common to every object or expression. Two of such properties are the  and   properties.

Immutable
In D,  is a storage-class just like. It converts a type to a non-modifiable type. Note that  means the same as   to the compiler.

Storage-Classes and
When you have a storage class like, you can omit   for type inference. The type of whatever is immutable is inferred from the compiler. This next code example is not valid because there is no way the compiler could infer the type:

Using Variables
This is allowed and perfectly fine code: It only sets  equal to the value of. does not have to be. That changes if you are taking a reference: You are allowed to cast the  away, but if you were to modify an immutable value using that hack, the result is undefined.

Strings as Arrays
You've seen strings since Lesson 1. A  is the exact same thing as , a dynamic array of immutable char elements. Likewise, a  is the same as , and a   is the same as.

String Properties
Strings have the same built-in properties as dynamic arrays. One useful property is the  property, for creating a mutable   copy of a string if you want to modify the individual characters of the string. The  property is useful for creating a copy of an existing string, or for creating a string copy of a.

Char Literals
A  literal is enclosed by single-quotes. There are also  and   literals.