On Tuesday, 20 July 2021 at 09:24:07 UTC, Mark Lagodych wrote:
Let's say I have this code:
```d
import std.stdio;

class X {

    int x(int param) {
        static int myvar = 1234;

        if (param == 0) return myvar;
        else { myvar = param; return myvar; }
    }

}

void main()
{
    X x1 = new X;
    X x2 = new X;

    x1.x(0).writeln;
    x2.x(0).writeln;

    x1.x(17).writeln;
    x2.x(0).writeln;
}
```

Is there a way to make myvar local to each instance of `X` without making it a variable of `X`? Just curious.

The only way I can think of is something like this:

```
import std.stdio;

interface X
{
    int x(int param);
}

class XImpl(size_t n = 0) : X {
    int x(int param) {
        static int myvar = 1234;

        if (param == 0) return myvar;
        else { myvar = param; return myvar; }
    }

}

void main()
{
    X x1 = new XImpl!1;
    X x2 = new XImpl!2;

    x1.x(0).writeln;
    x2.x(0).writeln;

    x1.x(17).writeln;
    x2.x(0).writeln;
}
```

Of course it's not entirely what you want but yeah I think that's about the closest to a solution that isn't overcomplicated.

Reply via email to