Newbie initial comments on D language - scope

Sean Kelly sean at f4.ca
Tue Jan 29 08:19:53 PST 2008


Edward Diener wrote:
> 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.

Someone may already have mentioned this, but 2.0 will eventually get
copy and destruction semantics for structs so it should be possible to
create a fairly decent smart pointer in D as well.


Sean



More information about the Digitalmars-d mailing list