auto, var, raii,scope, banana

Kirk McDonald kirklin.mcdonald at gmail.com
Thu Jul 27 12:06:51 PDT 2006


Sean Kelly wrote:
> John Reimer wrote:
> 
>> On Wed, 26 Jul 2006 02:52:06 -0700, Hasan Aljudy 
>> <hasan.aljudy at gmail.com> wrote:
>>
>>> vote++;
>>> //me likes
>>>
>>
>> I am sympathetic to the "scope" suggestion also.  People have to 
>> remember that Walter doesn't like adding any new keywords.  Reuse of 
>> "scope" may be the best solution here.  Are there any drawbacks? Does 
>> it fit the intended meaning and purpose appropriately?  It seems to.
> 
> 
> Given the suggested placement, I assume it would be illegal to reassign 
> a 'scope' reference to another object?  ie.
> 
>     class C {}
> 
>     scope C var = new C();
>     var = new C();
> 
> Assuming a reassignment is allowed, I assume the original object would 
> not be destroyed on scope exit?  Basically, I'm wondering whether the 
> 'scope' qualifier is attached to the reference or to the referent.
> 
> 
> Sean

Maybe we could decide that based on the keyword's placement:

class C {
     int m_i;
     this(int i) { m_i = i; }
     int i() { return m_i; }
}

scope C a = new C(1); // Destroy 'a' at end of scope
C b = a; // b is another reference to 1. It will be invalid at the
          // end of scope, except:
a = new C(2); // No, whoops! Destroy 2 at end of scope.
               // 1 is now referenced by b alone.
return b; // Allowed! The reference 'a' will no longer destroy 1.
return a; // NOT allowed! a is a scoped reference

The alternative:

C a = scope new C(1); // Destroy 1 at end of scope.
C b = a; // b is another reference to 1, which will be destroyed
          // at the end of scope.
a = new C(2); // A new, plain-old GCed instance.
return b; // NOT allowed! 1 is invalid after this scope.
return a; // Okay. a is a reference to a regular object.

Maybe we could allow both?

-- 
Kirk McDonald
Pyd: Wrapping Python with D
http://dsource.org/projects/pyd/wiki



More information about the Digitalmars-d mailing list