Newbie initial comments on D language - scope

Edward Diener eddielee_no_spam_here at tropicsoft.com
Mon Jan 28 20:07:59 PST 2008


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.

Given that the 'scope' class can not be passed around when it leaves the 
block in which it is created, the above would lead to an error. But I do 
not see how that affects my initial observation that one should not have 
to specify the 'scope' keyword on an object of a 'scope' class when 
declaring such an object, unless I misunderstand the use of 'scope' in 
that situation. Are you saying that without specifying 'scope' for an 
object of a 'scope' class the object does not behave as a 'scope' object 
and that therefore the above example you give does not destroy the 
object when the doThings function exits ? If that is the case, then I 
missed the ramifications of using 'scope' when referred to object 
declarations themselves.



More information about the Digitalmars-d mailing list