Newbie initial comments on D language - scope
Rioshin an'Harthen
rharth75 at hotmail.com
Tue Jan 29 00:39:46 PST 2008
"Janice Caron" <caron800 at googlemail.com> kirjoitti viestissä
news:mailman.33.1201592955.5260.digitalmars-d at puremagic.com...
> On Jan 29, 2008 1:50 AM, Edward Diener
> <eddielee_no_spam_here at tropicsoft.com> wrote:
>> 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.
I'll go somewhat deeper into this than Janice did.
This looks like it is a related issue to C# requiring the method-calling
place to note what parameters are ref and what parameters are out. The
compiler of course already knows which parameter is using which convention:
normal, ref, out... But the designers of C# think it's better to require
len += o.foo(bar, ref baz, out foobar);
than
len += o.foo(bar, baz, foobar);
because it documents the call better. Just by reading the call, it is
immediately know that baz is passed as a reference, which might change baz,
and that foobar will be used to return an additional value from the method.
No need to look up the definition of o.foo anywhere.
The same reasoning applies to forcing scoped classes to be marked as such at
the point of declaration in D. It requires for each instance the typing of
an extra keyword, but the readability of the code increases by such a factor
that typing that extra keyword doesn't bother me.
Now, you're reading through code that somebody else has written, having
inherited the maintaining of that code. There's a bad bug in it that has to
be fixed and quickly, because it's stopping the project from being
finished - and it was supposed to be ready a few weeks ago. You've managed
to tracke the bug to a certain function and look upon the code:
Foo f = new Foo;
// do whatever with f
return f;
Suppose D doesn't require that scope in front of Foo. You have to check the
Foo class, and you see the scope in front of the declaration. However, when
D requires the scope keyword, the code looks like:
scope Foo f = new Foo;
// do whatever with f
return f;
Bling! We see the error immediately, and have saved the need to actually
look for where the Foo class is defined, which could be in the middle of a
multi-kloc file that you couldn't have guessed from the ton of imports at
the top of the file this function resides in.
More information about the Digitalmars-d
mailing list