Newbie initial comments on D language - scope

Janice Caron caron800 at googlemail.com
Mon Jan 28 23:49:00 PST 2008


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).



More information about the Digitalmars-d mailing list