Debug help - Programming in D page 633 - concurrency shared(Exception) warnings

Steven Schveighoffer schveiguy at gmail.com
Sat Aug 30 18:04:20 UTC 2025


On Saturday, 30 August 2025 at 11:43:27 UTC, Brother Bill wrote:

> I tried various code changes, none of which worked.
> Kindly provide code changes to fix warnings that reflect modern 
> D programming.

This worked for me:

```d
import std.stdio;
import std.concurrency;
import std.conv;

struct CalculationFailure
{
     string reason;
}

struct Exit
{
}

void calculate()
{
     bool isDone = false;

     while (!isDone)
     {
         receive((string message) {
             try
             {
                 ownerTid.send(to!double(message) + 0.5);
             }
             catch (Exception exc)
             {
                 ownerTid.send(cast(shared)exc); // cast to shared 
to get it into the message queue.
             }
         },
             (Exit message) { isDone = true; });
     }
}

void main()
{
     Tid calculator = spawn(&calculate);
     calculator.send("1.2");
     calculator.send("hello"); // ← incorrect input
     calculator.send("3.4");
     calculator.send(Exit());

     foreach (i; 0 .. 3)
     {
         writef("result %s: ", i);
         receive(
             (double message) { writeln(message); },

             (shared Exception message) {
             throw cast()message; // cast away from shared to 
actually throw it.
         });
     }
}
```


More information about the Digitalmars-d-learn mailing list