On Sunday, 24 March 2024 at 07:41:41 UTC, Dom DiSc wrote:
I'm creating a library that is completely pure, but it doesn't compile with pure: at the top because of one impure unittest (which uses random to test some things only probabilistic)!

So do I really need to declare every function pure individually because of a test?!?

Can we please have a @impure attribute?
And by the way also @throws and @gc?
That would make live so much easier...

A better way to apply a attribute to an entire file is to use an explicit scope you can still apply this to basically the entire file but leave the tests out of it.

second since these are test its better for them to be pure, so use debug as it implicitly tells you where all global mutation happen, if you need your unit tests to run without debug for what ever reason fall back to suggestion one.

`dmd test.d -debug`

``` d
// File name test.d
import std.stdio;

int var = 0;

void main() {
  writeln(var);
  var = 1;
  writeln(var);
  foo();
  writeln(var);
  baz();
  writeln(var);
}

pure {

  int foo() {
    debug { var = 2;}
    return 2;
  }
  // you can nest scopes
  @safe {
    int bar () {
      return 3;
    }
  }
}

void baz() {
  var = 4;
}

```
  • Re: impure Jonathan M Davis via Digitalmars-d-learn
    • Re: impure Dom DiSc via Digitalmars-d-learn
      • Re: impure Jonathan M Davis via Digitalmars-d-learn
    • Re: impure MrJay via Digitalmars-d-learn
    • Re: impure Alexandru Ermicioi via Digitalmars-d-learn

Reply via email to