Newbie initial comments on D language - scope
Edward Diener
eddielee_no_spam_here at tropicsoft.com
Mon Jan 28 17:50:59 PST 2008
Sean Kelly wrote:
> Edward Diener wrote:
>> The 'scope' mechanism for RAII is a nice compromise for an intractable
>> general problem in GC languages, and I can see Walter Bright has
>> possibly been influenced by other GC languages that have sought to
>> address this issue. But a couple of areas seem really dubious to me.
>>
>> The first is the necessity of using an already scoped class by repeating
>> the 'scope' decalation when creating an object of that class. Since the
>> class itself has already been declared with the 'scope' keyword it seems
>> absolutely redundant that the user of an object of the class must repeat
>> 'scope' in his usage of that object. Surely the compiler is smart enough
>> to know that the class is a 'scope' class and will generate the
>> necessary code to automatically call the destructor of the class when it
>> goes out of scope. In fact the user of this class via an instantiated
>> object should not even care if it is a scoped class or not, so having to
>> say it is again seems doubly wrong, although allowable.
>
> I disagree. If the "scope" were not present at the point of
> declaration, I think it would be too easy for a maintainer of the code
> to screw up and return the scoped value. Requiring "scope" is akin to
> C++ having different declaration semantics for dynamic and static types.
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.
>
>> The second is that an object of a 'scope' class can only be instantiated
>> as a local variable of a function. That pretty much destroys the usage
>> of a 'scope' class ( aka a class encapsulating a resource which should
>> be released as soon as it is no longer referenced ) to the most narrow
>> of usages and means that nobody will bother actually creating such a
>> class for using RAII in D. Surely a 'scope' class should be instantiable
>> anywhere, with the obvious candidate being as a data member in an
>> enclosing class, which itself may or may not be scoped.
>
> Walter has mentioned in the past that he was considering doing exactly
> this, but like many other things I think it's been on the back-burner
> while const was sorted out in 2.0.
I understand.
More information about the Digitalmars-d
mailing list