Thread overview | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
August 05, 2013 With statement become like C#'s using? | ||||
---|---|---|---|---|
| ||||
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. |
August 05, 2013 Re: With statement become like C#'s using? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bosak | On Monday, 5 August 2013 at 12:40:26 UTC, Bosak wrote: > In C# there is this using construct: just declare variable as scope, and ti will be destructed as son as function exits. void foo () { scope obj = new Object; } // obj will be destructed here you can also use it together with anonymous scope (same as c#) more generally, there is a "scope" statement http://dlang.org/exception-safe.html that can be used where normally try/finally (without catch) would be used, to achieve cleaner code. |
August 05, 2013 Re: With statement become like C#'s using? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Michal Minich | Michal Minich:
> void foo ()
> {
> scope obj = new Object;
> } // obj will be destructed here
That usage of scope has being deprecated...
For Walter: I suggest dmd to give a deprecation message where you use one of the many deprecated D features, like scope classes, floating point comparison operators, and so on. Otherwise D programmers will use those featueres in their code today, and when those feature become deprecated, those people will become angry because of too much code to modify/fix.
Bye,
bearophile
|
August 05, 2013 Re: With statement become like C#'s using? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Monday, 5 August 2013 at 13:11:44 UTC, bearophile wrote:
> Michal Minich:
>
>> void foo ()
>> {
>> scope obj = new Object;
>> } // obj will be destructed here
>
> That usage of scope has being deprecated...
>
Why it is deprecated? I follow newsgroups, but that I miss...
I like it use together with scope classes: scope class C {} - are they too deprecated?
|
August 05, 2013 Re: With statement become like C#'s using? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Monday, 5 August 2013 at 13:11:44 UTC, bearophile wrote:
> Michal Minich:
>
>> void foo ()
>> {
>> scope obj = new Object;
>> } // obj will be destructed here
>
> That usage of scope has being deprecated...
>
> For Walter: I suggest dmd to give a deprecation message where you use one of the many deprecated D features, like scope classes, floating point comparison operators, and so on. Otherwise D programmers will use those featueres in their code today, and when those feature become deprecated, those people will become angry because of too much code to modify/fix.
>
> Bye,
> bearophile
It's not a feature I've ever had the need for so far, but what is the replacement for scope?
|
August 05, 2013 Re: With statement become like C#'s using? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Michal Minich | On Monday, 5 August 2013 at 12:49:11 UTC, Michal Minich wrote:
> On Monday, 5 August 2013 at 12:40:26 UTC, Bosak wrote:
>> In C# there is this using construct:
>
> just declare variable as scope, and ti will be destructed as son as function exits.
>
> void foo ()
> {
> scope obj = new Object;
> } // obj will be destructed here
>
> you can also use it together with anonymous scope (same as c#)
>
> more generally, there is a "scope" statement http://dlang.org/exception-safe.html that can be used where normally try/finally (without catch) would be used, to achieve cleaner code.
Oh yes, I completely forgot about scope.
|
August 05, 2013 Re: With statement become like C#'s using? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Monday, 5 August 2013 at 13:11:44 UTC, bearophile wrote:
> Michal Minich:
>
>> void foo ()
>> {
>> scope obj = new Object;
>> } // obj will be destructed here
>
> That usage of scope has being deprecated...
>
> For Walter: I suggest dmd to give a deprecation message where you use one of the many deprecated D features, like scope classes, floating point comparison operators, and so on. Otherwise D programmers will use those featueres in their code today, and when those feature become deprecated, those people will become angry because of too much code to modify/fix.
>
> Bye,
> bearophile
Interesting. I didn't knew that it is being deprecated. So my suggestion could actually be a good replacement of scope.?
|
August 05, 2013 Re: With statement become like C#'s using? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bosak | On Monday, 5 August 2013 at 12:40:26 UTC, Bosak wrote:
> with(Bitmap image = open("filename.bmp")) {
I think this shoudl be allowed simply for consistency with this:
if(auto a = getfoo()) { use a }
|
August 05, 2013 Re: With statement become like C#'s using? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Monday, 5 August 2013 at 13:33:28 UTC, Adam D. Ruppe wrote:
> On Monday, 5 August 2013 at 12:40:26 UTC, Bosak wrote:
>> with(Bitmap image = open("filename.bmp")) {
>
> I think this shoudl be allowed simply for consistency with this:
>
> if(auto a = getfoo()) { use a }
This is good. I think it should too.
But what Bosak proposes is that when "with" statements ends, the object should be destructed, which is large violation what with statements if for. Not mentioning breaking all the existing code...
|
August 05, 2013 Re: With statement become like C#'s using? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Michal Minich | On Monday, 5 August 2013 at 13:42:01 UTC, Michal Minich wrote:
> But what Bosak proposes is that when "with" statements ends, the object should be destructed
That would happen if the object is a struct, or wrapped in a struct, since it would go out of scope at the end of the with and call its destructor then.
So then you could just go
import std.typecons;
with(auto a = Scoped!T()) { ... }
and the Scoped destructor does the deleting.
|
Copyright © 1999-2021 by the D Language Foundation