automatic function call after closing block

Jarrett Billingsley jarrett.billingsley at gmail.com
Sat Sep 20 09:58:18 PDT 2008


On Sat, Sep 20, 2008 at 12:24 PM, Saaa <empty at needmail.com> wrote:
> Ah, that's at least nicer, but I meant that I somehow change texture.bind()
> to include texture.unbind()
>
> class texture
> {
> public void bind()
> {
>    ..
>    underlying scope(exit) texture.unbind()
> }
>
> private void unbind()
> {
>    ..
> }
>
> }
>
>
> ..
> texture.bind()
> {
>    ..
> }
> ..
>
>
>>
>> Yep!
>>
>> texture.bind()
>> {
>>    scope(exit) texture.unbind()
>>    ...
>> }
>
>
>

How about a horrible/wonderful misuse of the 'in' operator?  (credited
to Tom S):

class Texture
{
...
    struct BindBlock
    {
        Texture self;
        void opIn(void delegate() dg)
        {
            self.bind();
            scope(exit) self.unbind();
            dg();
        }
    }

    BindBlock bindBlock() { return BindBlock(this); }
...
}

auto t = new Texture("foo.png");

t.bindBlock() in
{
    // some rendering code!
};


It has a nice functional-sounding flair to it, with the 'in' there ;)


More information about the Digitalmars-d-learn mailing list