On Wednesday, 11 May 2022 at 20:53:21 UTC, Marvin Hannott wrote:
On Wednesday, 11 May 2022 at 20:23:07 UTC, Ali Çehreli wrote:
[...]

Yeah, but you can't return `Cat` 😉. And the documentation for `scoped` says:
[...]

That's kinda very limiting.

Anyway, I cooked up another idea based on your first suggestions.
```D
struct S
{
    static private interface I
    {
        int f(int i);
    }

    static private final class A : I
    {
        int f(int i) {return i;}
    }

    static private final class B : I
    {
        int f(int i) {return i+ 1;}
    }

    private I i;
    private int d;

    this(int d)
    {
        this.d = d;
        if(d < 10)
        {
            i = scoped!A();
        }else {
            i = scoped!B();
        }
    }

    int f() { return i.f(d);}
}
```
I mean, this is a super dumb example, but it kinda works. And I think it could be made a lot less tedious with some mixin magic.

add a single `writeln("A.f")` to A.f and `writeln("B.f")` B.f, and a simple test

```d
    void main()
    {
        S s1 = S(9);
        S s2 = S(12);
        s1.f();
        s2.f();
    }
```

outputs:

```
A.f
Error: program killed by signal 11
(Aka.  segmentation fault !)
```

Reply via email to