www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Is it true scope declarations are being removed?

reply Sean Eskapp <eatingstaples gmail.com> writes:
Someone mentioned to me that scope declarations, e.g.

scope class A{}
or
scope A myNewObject;

are being removed from the language. Is this true? If so, how will RAII-type
classes be implemented?
Jan 21 2011
next sibling parent reply Trass3r <un known.com> writes:
Sadly true.
They intend to replace it with a library based solution, I don't know why.
Jan 23 2011
next sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
I don't understand it either. AFAIK they are being removed because
they're unsafe, and are being replaced by an unsafe library solution.
Jan 23 2011
next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Andrej Mitrovic:

 I don't understand it either. AFAIK they are being removed because
 they're unsafe, and are being replaced by an unsafe library solution.
I have hated see typedef and scoped classes go (I have even missed delete), but you need a bit of faith in the future and in Andrei & Walter. Andrei is not evil, and he's smart. D2 language is a very young language, and when a built-in feature looks not perfect, it's better to remove it now. If you remove it, you will have plenty of time in future to add it back, add something better implementation of it, or to find a better and very different solution, or even to add a more general language feature that allows you to implement the original half-broken feature in library code. While if something badly designed is left in the language, then you are struck with it forever, or almost forever. Generally in language it's 10-100 times simpler to add a new feature than to remove it :-) Better to start with a not complete D2 language, a language with holes waiting to be filled, that with a language with unfixable warts that you may "fix" just adding another better feature and pretending the old one doesn't exist any more (example: nullptr of C++0x). Bye, bearophile
Jan 23 2011
parent Kagamin <spam here.lot> writes:
bearophile Wrote:

 If you remove it, you will have plenty of time in future to add it back, add
something better implementation of it, or to find a better and very different
solution, or even to add a more general language feature that allows you to
implement the original half-broken feature in library code.
Having plenty of time, algol must be perfect now.
Jan 24 2011
prev sibling parent Trass3r <un known.com> writes:
 I don't understand it either. AFAIK they are being removed because
 they're unsafe, and are being replaced by an unsafe library solution.
I have the same feeling. While I do understand why typedef is poorly designed (http://d.puremagic.com/issues/show_bug.cgi?id=5467) I can't imagine how a library-based scope is any better than before.
Jan 23 2011
prev sibling parent reply so <so so.do> writes:
On Mon, 24 Jan 2011 00:11:48 +0200, Trass3r <un known.com> wrote:

 Sadly true.
 They intend to replace it with a library based solution, I don't know  
 why.
If the library solution is as good as the original, it is a big plus. If only we could do the same for everything!
Jan 23 2011
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Sun, 23 Jan 2011 19:03:19 -0500, so <so so.do> wrote:

 On Mon, 24 Jan 2011 00:11:48 +0200, Trass3r <un known.com> wrote:

 Sadly true.
 They intend to replace it with a library based solution, I don't know  
 why.
If the library solution is as good as the original, it is a big plus. If only we could do the same for everything!
In fact the library solution will be better, because it's an expression, not a storage class. This means, you can "scope" a class inside another statement instead of having to declare/initialize it separately. It solves a bug I filed: http://d.puremagic.com/issues/show_bug.cgi?id=2070 -Steve
Jan 24 2011
parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 1/24/11, Steven Schveighoffer <schveiguy yahoo.com> wrote:
 This means, you can "scope" a class inside another statement instead of
 having to declare/initialize it separately.  It solves a bug I filed:

 http://d.puremagic.com/issues/show_bug.cgi?id=2070

 -Steve
That is pretty cool. What I worried about is that a library solution could mean loosing help from the compiler itself. E.g. if the compiler sees a scoped variable it can do some checks to see that you're not escaping a reference by accident, inadvertently assigning it to a global, or some other sanity checks. I guess these things might be doable in a library but I don't know to what extent.
Jan 24 2011
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 24 Jan 2011 09:41:30 -0500, Andrej Mitrovic  
<andrej.mitrovich gmail.com> wrote:

 On 1/24/11, Steven Schveighoffer <schveiguy yahoo.com> wrote:
 This means, you can "scope" a class inside another statement instead of
 having to declare/initialize it separately.  It solves a bug I filed:

 http://d.puremagic.com/issues/show_bug.cgi?id=2070

 -Steve
That is pretty cool. What I worried about is that a library solution could mean loosing help from the compiler itself. E.g. if the compiler sees a scoped variable it can do some checks to see that you're not escaping a reference by accident, inadvertently assigning it to a global, or some other sanity checks. I guess these things might be doable in a library but I don't know to what extent.
That wasn't done by the compiler anyways. -Steve
Jan 24 2011
next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 1/24/11, Steven Schveighoffer <schveiguy yahoo.com> wrote:
 That wasn't done by the compiler anyways.

 -Steve
Yeah I've noticed that. Lazy DMD compiler! :)
Jan 24 2011
prev sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Monday 24 January 2011 07:49:44 Andrej Mitrovic wrote:
 On 1/24/11, Steven Schveighoffer <schveiguy yahoo.com> wrote:
 That wasn't done by the compiler anyways.
 
 -Steve
Yeah I've noticed that. Lazy DMD compiler! :)
Much of anything that requires code flow analysis doesn't tend to happen. And it's so easy to get around it in many cases (such as passing a pointer to a function which then returns that pointer) that it wouldn't ultimately help all that much anyway (though having _some_ is still better than nothing). The fact that such things can't be reliably verified at compile time is exactly why scoped classes are unsafe in the first place. - Jonathan M Davis
Jan 24 2011
prev sibling next sibling parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Friday 21 January 2011 06:10:26 Sean Eskapp wrote:
 Someone mentioned to me that scope declarations, e.g.
 
 scope class A{}
 or
 scope A myNewObject;
 
 are being removed from the language. Is this true? If so, how will
 RAII-type classes be implemented?
std.typecons.scoped is the replacement for the builtin scope declaration. scope declarations are going away because they're inherently unsafe. Why they are then being put into the library, I'm not quite sure, other than the fact that D doesn't _stop_ you from doing unsafe stuff, it just seriously discourages it. I suppose that the big problem with having it in the language itself that the library solution doesn't quite have is that having it in the language kind of implies that it's okay and reasonable to do, which it isn't. It makes sense in specific circumstances, where you're know what all of the code involved is doing and can be sure that no reference to the scoped class will escape. And even then, why didn't you just make it a struct in the first place? There _are_ uses for scoped classes, but they're really not supposed to be the norm. They're unsafe, and you're begging for bugs if you use them heavily. - Jonathan M Davis
Jan 23 2011
parent reply Trass3r <un known.com> writes:
 scope declarations are going away because they're inherently unsafe.
Pointers are inherently unsafe as well ;)
Jan 24 2011
parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Monday 24 January 2011 00:37:40 Trass3r wrote:
 scope declarations are going away because they're inherently unsafe.
Pointers are inherently unsafe as well ;)
Well, you can't take those out of the core language and put them in the standard library. And there _are_ cases where pointers are perfectly safe. It's pointer arithmetic and taking the addresso of a local variable which is unsafe. Those are going to be restricted to SafeD. Andrei decided that scope was too unsafe to have in the language (and presumably Walter agreed) and it _can_ be put into the standard library. If you have a problem with it, take it up with him. - Jonathan M Davis
Jan 24 2011
prev sibling parent bearophile <bearophileHUGS lycos.com> writes:
Jonathan M Davis:

 Much of anything that requires code flow analysis doesn't tend to happen.
Right. You need (lot of) work to implement it, in a system language it can't be fully accurate, and it may slow down compilation a little (Scala compiler is powerful, but I don't think it's very fast). But from using C lints I've seen that catching simple cases is better than catching none of them, and in SafeD the code is less wild so probably a higher percentage of cases can be caught. Currently DMD catches only very simple errors: class Foo {} Foo foo() { scope Foo f = new Foo; return f; // test.d(4): Error: escaping reference to scope local f } Foo bar() { scope Foo f = new Foo; Foo f2 = f; return f2; // no error here } void main() {} Something similar happens with this little C program compiled with -Wall with GCC: int *foo() { int a[5]; return a; } int *bar() { int a[5]; int *p = a; return p; } int main() { return 0; } GCC warns only about the first function: test.c: In function 'foo': test.c:3:5: warning: function returns address of local variable But a good C lint catches them both, statically (and the checking is very fast): test.c 3 Warning 604: Returning address of auto variable 'a' test.c 8 Warning 674: Returning address of auto through variable 'p' Bye, bearophile
Jan 24 2011