Newbie initial comments on D language - scope

Edward Diener eddielee_no_spam_here at tropicsoft.com
Mon Jan 28 16:51:31 PST 2008


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.

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.

The usage of the 'scope' keyword still would have a very important
function if it is designated to force RAII on an instantiated object
which would not ordinarily be scoped. This could occur most naturally
when the programmer is creating any container which may have scoped
objects in it, including the D versions of static and dynamic arrays and
associated arrays. In this way both the class designer can implement
RAII in his class and the programmer can implement RAII on their objects
independently of each other, with both having the necessary control to
solve the resource problem as far as the idea of a scope allows.




More information about the Digitalmars-d mailing list