Re: With statement become like C#'s using?

2013-08-06 Thread Jacob Carlborg
On 2013-08-05 14:40, Bosak wrote: 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

Re: With statement become like C#'s using?

2013-08-05 Thread Andrei Alexandrescu
On 2013-08-05 17:45:23 +, Bosak said: I think this dispose thing should be added to D, because it is a very common practice in C#(and not only). In the book I used to learn C# long ago I remember how they sayed like 100 of times: "If you use an object that implements IDisposable ALLWAYS use

Re: With statement become like C#'s using?

2013-08-05 Thread Gambler
On 8/5/2013 11:30 AM, Bosak wrote: > On Monday, 5 August 2013 at 13:54:38 UTC, Adam D. Ruppe wrote: >> 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 o

Re: With statement become like C#'s using?

2013-08-05 Thread Bosak
On Monday, 5 August 2013 at 18:29:09 UTC, Dicebot wrote: On Monday, 5 August 2013 at 17:45:25 UTC, Bosak wrote: You say that D's destroy is not like C#'s IDisposable.Then why doesn't D then declare that kind of interface: interface Disposable { void dispose(); } It _is_ similar but not exa

Re: With statement become like C#'s using?

2013-08-05 Thread Dicebot
On Monday, 5 August 2013 at 17:45:25 UTC, Bosak wrote: You say that D's destroy is not like C#'s IDisposable.Then why doesn't D then declare that kind of interface: interface Disposable { void dispose(); } It _is_ similar but not exact match. 2 key differences: 1) destroy works on variety

Re: With statement become like C#'s using?

2013-08-05 Thread Andre Artus
--snip-- Bosak: class Resource { //can be any resource from files to streams to anything Resource[] used; void opDispose() { writeln("Resource disposed!"); You should avoid doing IO in a destructor/finaliser. Writing to STDOUT can fail which may lead to resource leaks (if

Re: With statement become like C#'s using?

2013-08-05 Thread Bosak
On Monday, 5 August 2013 at 15:38:12 UTC, Dicebot wrote: On Monday, 5 August 2013 at 15:30:45 UTC, Bosak wrote: ... What you propose is rather unwelcome for classes (destruction of GC-managed objects is non-deterministic) and closest thing for IDisposable D has is "destroy" which is not some

Re: With statement become like C#'s using?

2013-08-05 Thread Dicebot
On Monday, 5 August 2013 at 15:55:00 UTC, John Colvin wrote: On Monday, 5 August 2013 at 14:00:47 UTC, Dicebot wrote: On Monday, 5 August 2013 at 13:25:43 UTC, John Colvin wrote: It's not a feature I've ever had the need for so far, but what is the replacement for scope? http://dlang.org/phob

Re: With statement become like C#'s using?

2013-08-05 Thread John Colvin
On Monday, 5 August 2013 at 14:00:47 UTC, Dicebot wrote: On Monday, 5 August 2013 at 13:25:43 UTC, John Colvin wrote: It's not a feature I've ever had the need for so far, but what is the replacement for scope? http://dlang.org/phobos/std_typecons.html#.scoped cool, thanks. Any particular ca

Re: With statement become like C#'s using?

2013-08-05 Thread Dicebot
On Monday, 5 August 2013 at 15:30:45 UTC, Bosak wrote: ... What you propose is rather unwelcome for classes (destruction of GC-managed objects is non-deterministic) and closest thing for IDisposable D has is "destroy" which is not something that is expected to be called silently. However,

Re: With statement become like C#'s using?

2013-08-05 Thread Bosak
On Monday, 5 August 2013 at 13:54:38 UTC, Adam D. Ruppe wrote: 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

Re: With statement become like C#'s using?

2013-08-05 Thread Dicebot
On Monday, 5 August 2013 at 13:25:43 UTC, John Colvin wrote: It's not a feature I've ever had the need for so far, but what is the replacement for scope? http://dlang.org/phobos/std_typecons.html#.scoped

Re: With statement become like C#'s using?

2013-08-05 Thread Adam D. Ruppe
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 d

Re: With statement become like C#'s using?

2013-08-05 Thread Michal Minich
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

Re: With statement become like C#'s using?

2013-08-05 Thread Bosak
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

Re: With statement become like C#'s using?

2013-08-05 Thread Adam D. Ruppe
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 }

Re: With statement become like C#'s using?

2013-08-05 Thread John Colvin
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

Re: With statement become like C#'s using?

2013-08-05 Thread Bosak
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 wi

Re: With statement become like C#'s using?

2013-08-05 Thread Michal Minich
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 cl

Re: With statement become like C#'s using?

2013-08-05 Thread bearophile
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 ope

Re: With statement become like C#'s using?

2013-08-05 Thread Michal Minich
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 anonymo

With statement become like C#'s using?

2013-08-05 Thread Bosak
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.