In C# there is this using construct:

using(Bitmap image = this.OpenImage("filename.bmp")) {
    image.Name = "foo";
    //use image like image.sth
}

which is translated to:

{
    Bitmap image = this.OpenImage("filename.bmp");
    try {
        image.Name = "foo";
        //use image like image.sth
    }
    finally {
        IDisposable obj = image as IDisposable;
        if(obj != null)
            obj.Dispose();
    }
}

I know that the with statement is different, but it can be improved so that you can declare things in it like an using statement:

with(Bitmap image = open("filename.bmp")) {
    name = "foo";
    //no need to specify image.sth
}

or even a more implicit one:
with(open("filename.bmp")) {
//ditto
}

And both of the above to be translated to:

{
    Bitmap temp = expression;
    //use bitmap
    delete temp; // Call destructor/finallizer of the object
//I'm not sure if delete was the proper way to call a destructor in D
}

And I hope you got the point. Tell me what you think.

Reply via email to