automatic function call after closing block

Michel Fortin michel.fortin at michelf.com
Sat Sep 20 09:31:44 PDT 2008


On 2008-09-20 12:15:17 -0400, "Saaa" <empty at needmail.com> said:

> 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