Newbie initial comments on D language - scope

Bill Baxter dnewsgroup at billbaxter.com
Tue Jan 29 19:32:11 PST 2008


Edward Diener wrote:
> Janice Caron wrote:
>> On Jan 29, 2008 1:50 AM, Edward Diener
>> <eddielee_no_spam_here at tropicsoft.com> wrote:
>>> 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.
>>
>> What you're suggesting is "semantic sugar" - allowing the compiler to
>> save us a bit of typing. Sometimes, that can be a good thing. Here,
>> however, I don't think it would be. You see, while the /compiler/
>> knows that Foo is RAII (you're right about that), future maintainers
>> of the function might not. Forcing the use of the keyword makes the
>> code a bit more readable.
>>
>> Here's another way of looking at it: The right hand side of the
>> statement is evaluated /first/. Then it is assigned to the lvalue. So,
>> when the RHS is evaluated (new Foo()) it returns a value whose type is
>> "scope Foo". Now, you can't assign a "scope Foo" to a "Foo", so the
>> assignment fails. Allowing the semantic sugar would be like
>> "storage-class-deduction", which would open up a really huge can of
>> worms, and we almost certainly don't want to go there (at least not
>> before const has settled down).
> 
> I see it exactly the other way. It is semantic sugar to force a 
> programmer to specify that the instantiation of a scope class creates a 
> scope object, just for the sake of making future maintainers feel 
> better. One should not really care that a class is a scope class. It 
> should just work to release the resource it encompasses when it goes out 
> of scope by having its destructor called.
> 
> In a GC environment memory is just another resource. The user of objects 
> does not worry about memory being released as appropriate. Why should he 
> have to worry about other resources being released as appropriate ?
> 
> Understand that I am not saying that the user of an object of a scope 
> class can not benefit from knowing, if he chooses, that the class is a 
> scope class. Part of my suggestion about the keyword 'scope' in D is 
> that when used it should force any object, even not normally scoped, to 
> be destroyed when it goes out of scope. In this way the programmer can 
> force a container of scoped objects to be destroyed immediately when it 
> goes out of scope even though the container is not a scoped type.

My opinion is that the current rules make scope classes of very limited 
use.  Regular classes can still be used 'scope'd if desired, so all 
you're doing by declaring the class itself scope is removing the users' 
freedom to choose.

The only reason to use it is if you have a class that absolutely must 
not persist longer than one stack frame.  If you don't have such a 
requirement then you might as well not bother with scope classes.  Let 
users decide whether they want it to be scope or not.

--bb



More information about the Digitalmars-d mailing list