Listings Hanna/D

Listing 1: Informationen in den Datentyp packen
myCplxDouble.re = rT;
myCplxDouble.im = iT;
writeln("C lautet " , myCplxDouble);
writeln("Ihre Länge ist ", myCplxDouble);
readf(" %s", &iT);
}

-------

Listing 2: Konstruktor mit dem Schlüsselwort super aufrufen
class A { this() { ... } }
class B : A
{
    this(int x)
    {
        ...
        super();  // call base constructor
        ...
    }
}

-------

Listing 3: Größe eines Arrays festlegen
void main() {
int[] myA=new int[3];
myA.length = myA.length + 1;
myA[myA.length - 1] = 2;
writeln(myA[0], " ", myA[3]);
}

-------

Listing 4: Die Aufnahme von Logik
struct S1 {
int myF;
this(int _m){ myF = _m; }
void echoThis(){writeln("Struct enthält ", myF);}
}

-------

Listing 5: Interfaces einbinden
interface MyIF {
void echoT();
}

class CErb:C1, MyIF {
. . .
final override void echoThis() {
write("CErb sagt HALLO ");
super.echoT();
}
}

-------

Listing 6: foreach-Schleifen parallelisieren
import std.parallelism;
. . .

void main() {
    auto s = [ Work(1), Work(2), Work(3), Work(4) ];

    foreach (Work; parallel(s)) {
        s.warte2Sec();
    }
}

-------

Listing 7: Gleichzeitiges Einlesen einer Datei
import std.file;

void main()
{
    auto file1Task = task!read("foo.txt");
    file1Task.executeInNewThread();
    auto file2Data = read("bar.txt");
    auto file1Data = file1Task.yieldForce;
}

-------

Listing 8: Postkästen warten auf Nachrichten
void main() {
    Tid worker = spawn(&workerFunc);
    foreach (value; 1 .. 5) {
        worker.send(value);
        double result = receiveOnly!double();
        writefln("sent: %s, received: %s", value, result);
    }
    worker.send(-1);
}

-------

Listing 9: Synchronisationsprimitivum
foreach (i; 0 .. 10_000) {
    synchronized {
        int temp = *b;
        *b = *a;
        *a = temp;
    }
}

