automatic function call after closing block

Saaa empty at needmail.com
Sat Sep 20 10:40:22 PDT 2008


Never used/heart of a scope class, thanks!
But I'll try the delegate thing first ;)


>
>> I'd like the following:
>>
>> texture.bind() //class texture, method bind.
>> {
>>     ..
>> } //here texture.unbind would automatically be run.
>>
>> Is this possible?
>
> You could create a scope class, bind in the constructor and unbind in the 
> destructor. Something like this:
>
> scope class Binding {
> Texture t;
> this(Texture t) { this.t = t; t.bind() };
> ~this() { t.unbind(); }
> }
>
> then use it like this:
>
> {
> scope b = new Binding(texture);
> ...
> // automatic destruction of b;
> }
>
> Perhaps it could be made more elegant in D2 with struct destructors.
>
> -- 
> Michel Fortin
> michel.fortin at michelf.com
> http://michelf.com/
> 




More information about the Digitalmars-d-learn mailing list