Newbie initial comments on D language - scope

Leandro Lucarella llucax at gmail.com
Tue Jan 29 07:51:07 PST 2008


Edward Diener, el 28 de enero a las 23:07 me escribiste:
> Jesse Phillips wrote:
> >On Mon, 28 Jan 2008 20:50:59 -0500, Edward Diener wrote:
> >>I do not understand what you mean by "return the scoped value". If in D
> >>I write:
> >>
> >>scope class Foo { ... }
> >>
> >>then why should I have to write, when declaring an instance of the
> >>class:
> >>
> >>scope Foo g = new Foo();
> >>
> >>as opposed to just:
> >>
> >>Foo g = new Foo();
> >>
> >>The compiler knows that Foo is a scoped class, so there is no need for
> >>the programmer to repeat it in the object declaration.
> >He is referring to when you have:
> >scope class Foo() {}
> >Foo doThings() {
> >   Foo cats = new Foo();
> >   return cats;
> >}
> >cats no longer exists after return.
> 
> Yes, I can see that. My own idea of a 'scope' class in a GC environment, one that completely solves the RAII conundrum, is that one should be able to pass 
> around an object of that class and when the last reference to that object goes out of scope the destructor is immediately called.
> 
> That is very much like what boost::shared_ptr<T> offers for C++ in a language which does not have GC, but it is probably harder to implement in a GC language 
> where such checks are ordinarily not made when an object goes out of scope.

Exactly, that's a reference count, another way of doing *GC*, and that has
a lot of other complexities (like circular dependencies) and overhead (the
counting itself). Scope is much simpler, and as the name says, it destroys
an object when it's out of scope, just like C++ does with any object
allocated in the stack.

Anyway, I think I remember Walter saying that he wanted to add 2.0 the
tools necessary to smoothly implement reference counting.

-- 
Leandro Lucarella (luca) | Blog colectivo: http://www.mazziblog.com.ar/blog/
----------------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------------
1 cigarette takes away 5 minutes of a person's life



More information about the Digitalmars-d mailing list