On Friday, 11 May 2018 at 15:03:41 UTC, Uknown wrote:
I see what you're saying and I agree with you. I think a better way would be to try and extend the `with` syntax to work with arbitrary functions, rather than only objects. That would make it more useful. So something like:

---
void f1(allocator alloc, ...){}
void f2(allocator alloc, ...){}
...
void fn(allocator alloc, ...){}

void main()
{
    with(MyAllocator) {
        f1(...);
        f2(...);
        ...
        fn(...);
    }
}
---

It's not as pretty, and I don't know if it works outside this toy example yet, but you can do:

import std.stdio;

struct Allocator
{
    auto call(alias F, Args...)(Args args)
    {
        return F(this, args);
    }

    void deallocateAll()
    {
        writeln("deallocateAll");
    }
}

void f1(Allocator a, int n) { writeln("f1"); }
void f2(Allocator, string s, double d) { writeln("f2"); }

void main()
{
    with (Allocator())
    {
        scope(exit) deallocateAll;
        call!f1(2);
        call!f2("asdf", 1.0);
    }
}

Reply via email to