Newbie initial comments on D language - scope

Edward Diener eddielee_no_spam_here at tropicsoft.com
Sat Feb 2 13:41:10 PST 2008


Walter Bright wrote:
> Edward Diener wrote:
>> The idea in my mind is essentially that 'scope' classes automatically 
>> define an object that, when used exactly as an normal GC object, 
>> automatically calls the destructor of the object just as soon as the 
>> last reference to it goes out of scope. In this sense the user neither 
>> know or cares whether the object encapsulates a resource or not and 
>> uses an object of such a class just as he would use any other GC 
>> object. Similarly the user can force an object to be a 'scope' object 
>> through the second syntax give above, but from then on he treats the 
>> object just as he would any other GC object.
> 
> The problem is:
>     scope C c;
>     C d;
>     d = c;
> and now d is no longer properly ref-counted. The 'scopeness' of an 
> object must therefore be part of its type. The assignment d=c would have 
> to be illegal.

My brain was not working correctly earlier, so let me correct myself.

In your above, if the c object is 'scope', whether it is because the C 
class is 'scope' or, as in your example, you specify 'scope' on the 
object ( which in current D is the same thing as saying that the C class 
is 'scope' ) then the assignment to another object makes that object 
'scope' automatically. This is yet another reason why 'scope' at the 
compiler should be tracked at the object level, not at the class level. 
The canonical situation is:

class C { ... }
scope class D : C { ... }

scope ( redundant IMO ) D  d = new D(...);
C c = d;

Clearly c, whose polymorphical type is a D, has to be 'scope'.



More information about the Digitalmars-d mailing list