Newbie initial comments on D language - scope

Bruce Adams tortoise_74 at yeah.who.co.uk
Tue Jan 29 03:42:55 PST 2008


On Tue, 29 Jan 2008 08:39:46 -0000, Rioshin an'Harthen  
<rharth75 at hotmail.com> wrote:

> 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.
>
Unless you need to know. What is does and what the pre and post conditions
are. The idea of deliberately programming blind does not appeal to me.
Extra documentation as a style option on the otherhand sounds reasonable.

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

Any halfway decent compiler should report that as a semnatic error and  
refuse
to compile it. If the error message is specific enough repeating the  
keyword
or not makes no odds. As Janice says type (storage class) deduction may be  
a
more serious can of worms.





More information about the Digitalmars-d mailing list