| Topics: | Language and Phobos -DGui (example placeholder - can be any library) | ||
| Previous | Lessons: | 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -T | Next |
In this chapter, you will learn about strings in depth. In the meantime, you will also learn about another type: the dynamic array.
importstd.stdio;voidmain(){int[]a=[1,2,3,4];int[]b=[5,6];autoc=a~b;writeln(c);// [1,2,3,4,5,6]writeln(c.length);// 6int*ptr_c=c.ptr;ptr_c[0]=3;writeln(c);// [3,2,3,4,5,6]}
importstd.stdio;voidmain(){// Concept: Immutableimmutable(int)a=10;// a = 11; Error: cannot modify immutableimmutablea=10;// Concept: Strings as Arraysimmutable(char)[]str="Hello";autostr1=str~"1";writeln(str1);// Hello1writeln(str1.length);// 6// str1[] = 'z'; error! str1's elements are not mutablechar[]mutablestr1=str1.dup;// Concept: Char Literalsmutablestr1[]='z';// 'z' is not a string, but a charstr1=mutablestr1.idup;// The str1 itself is mutable// only its elements are not mutablewriteln(str1);// zzzzzz}
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:
int[]a;
You can create a dynamic array initiated with a specific length:
int[]a=newint[](5);
You will learn more about thenew keyword later.
The syntax for filling an array with a single value is the same for both static and dynamic arrays:
int[]a=[1,2,3];a[]=3;writeln(a);// [3, 3, 3]
Dynamic arrays are indexed in the same fashion as static arrays. The syntax for accessing an element at a certain index is the same:
a[2];
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:
int[]a=[1,2,3];writeln(a[100]);//Runtime error, Range violation
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.
int[]a=[1,2];autob=a~[3,4];writeln(b);//[1, 2, 3, 4]b~=5;// same as b = b ~ 5;writeln(b);//[1, 2, 3, 4, 5]
You shouldn't assign a dynamic array to a static array (my_static_arr = my_dynamic_arr;) unless if you are certain thatmy_dynamic_arr.length is the equal tomy_static_arr.length. 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.
int[]a=[9,9,9,9,9,9];int[3]b=[1,2,3];int[200]c;a=b;writeln(a);// [1, 2, 3]a=c;writeln(a.length);// 200int[]d=[5,4,3];b=d;// OK: lengths are both 3// c = d; runtime error! lengths don't match.
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.
voidtryToChangeLength(int[]arr){arr.length=100;}voidmain(){int[]a=[1,2];tryToChangeLength(a);writeln(a.length);// still 2}
You learned in the last chapter that you can cause things to be passed by reference instead of by value by adding theref modifier.
These properties apply to both static and dynamic arrays:
| Property | Description |
|---|---|
| .init | For static arrays, it returns an array with each element initialized to its default value. |
| .sizeof | Returns the size of the array in memory. |
| .length | Returns the number of elements in the array. This is fixed in static arrays. |
| .ptr | Returns a pointer to the first element of the array. |
| .dup | Creates a dynamic array that is a copy of the array and returns it. |
| .idup | Similar to.dup but the copy's elements areimmutable. |
| .reverse | Returns the array with its elements in reverse order. |
| .sort | Sorts in place the elements in the array and returns the result. |
There are also other properties which are common to every object or expression. Two of such properties are the.init and.sizeof properties.
In D,immutable is astorage-class just likeauto. It converts a type to a non-modifiable type.
immutable(int)fixed_value=37;immutableintanother_value=46;
Note thatimmutable(type) means the same asimmutable type to the compiler.
autoWhen you have a storage class likeimmutable, you can omitauto for type inference.
immutablefixed_value=55;
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:
immutablefixed_value;//Error!
immutable VariablesThis is allowed and perfectly fine code:
immutable(int)a=300;intb=a;
It only setsb equal to the value ofa.b does not have to beimmutable. That changes if you are taking a reference:
immutable(int)a=13;immutable(int)*b=&a;// int* c = &a; Error.
You are allowed to cast theimmutable away, but if you were to modify an immutable value using that hack, the result is undefined.
immutable(int)a=7;int*b=cast(int*)&a;// Just make sure you do not modify a// through b, or else!
You've seen strings since Lesson 1. Astring is the exact same thing asimmutable(char)[], a dynamic array of immutable char elements. Likewise, awstring is the same asimmutable(wchar)[], and adstring is the same asimmutable(dchar)[].
Strings have the same built-in properties as dynamic arrays. One useful property is the.dup property, for creating a mutablechar[] copy of a string if you want to modify the individual characters of the string.
stringa="phobos";char[]b=a.dup;b[1]='r';b[4]='e';writeln(b);// probes
The.idup property is useful for creating a copy of an existing string, or for creating a string copy of achar[].
stringa="phobos";stringcopy_a=a.idup;char[]mutable_a=copy_a.dup;mutable_a[3]='t';copy_a=mutable_a.idup;writeln(mutable_a);// photoswriteln(a);// phobos
Achar literal is enclosed by single-quotes. There are alsowchar anddchar literals.
autoa="a";// stringautob='b';// charautoc='c'c;// charautod='d'w;// wcharautoe='e'd;// dchar