On Wednesday, 16 April 2025 at 14:48:04 UTC, H. S. Teoh wrote:
Pointers can be used perfectly fine in @safe. Just not unsafe operations like pointer arithmetic.


So, apart from stdout.flush(), is it perfect to use the following code with @safe:

```d
import std.stdio, std.random;
import std.random : ups = unpredictableSeed;
import core.stdc.stdlib : rand, RAND_MAX;

alias T = double; // float;
enum N = int.max; // long.max;
auto phobosRandom()
  => cast(T)Random(ups).front / N;

auto stdlibRandom()
  => cast(T)rand() / RAND_MAX;

@safe:
struct RandGroup
{
    string name;
    T function() gene;
    T up = 0.9;
    T down = 0.1;
} /* Prints:

0.10826: Testing uniform...
0.493747: Testing Phobos...
0.840188: Testing StdLib...
//*/

void main()
{
    RandGroup[3] funcs = [
    {
          name: "uniform",
          gene: () => uniform(0.0, 1.0),
          //down: 0.01, up: 0.02
    }, {

          name: "Phobos",
          gene: &phobosRandom,

    }, {

          name: "StdLib",
          gene: &stdlibRandom,

     },
  ];
  
  foreach (func; funcs)
  {
    with(func)
    {
      auto generate = gene();
      generate.write(": ");
      writeln("Testing ", name, "...");
      //stdout.flush(); // error
    }
  }
}
```

SDB@79


Reply via email to