Docs: Section on local variables

Nick Sabalausky SeeWebsiteToContactMe at semitwist.com
Fri Apr 20 02:55:19 PDT 2012


"Jakob Ovrum" <jakobovrum at gmail.com> wrote in message 
news:lehdgtfkqawadxgsgymv at forum.dlang.org...
> On Friday, 20 April 2012 at 01:22:53 UTC, Andrej Mitrovic wrote:
>> Can we remove this section from the D docs, in the functions section? :
>>
>> "
>> Local Variables
>> It is an error to use a local variable without first assigning it a
>> value. The implementation may not always be able to detect these
>> cases. Other language compilers sometimes issue a warning for this,
>> but since it is always a bug, it should be an error.
>>
>> It is an error to declare a local variable that is never referred to.
>> Dead variables, like anachronistic dead code, are just a source of
>> confusion for maintenance programmers.
>> "
>>
>> I don't think this will ever be implemented, or that it should be for
>> that matter. The first paragraph is quite extreme.
>
> Many other languages have this restriction on local variables and
> I would love to see DMD do the same kind of analysis. You can't
> force programmers to write readable code, but you should at least
> discourage it; the current way of default-initializing local
> variables and using that as an excuse to not have flow analysis
> is extremely weak and just reflects an unwillingness of the DMD
> developers to take on this task, affecting the design of the
> language.
>
> I have seen this one in the wild before:
>
> for(size_t i; i < arr.length; i++) {
>      ...
> }
>
> This shouldn't be correct D code, and I think it's one of D's few
> weaknesses that it is. Removing this paragraph from the
> specification would be designing the language around DMD instead
> of the other way around, and I really don't want to see that.
>

It's funny, I once argued strongly in favor of this *against* Walter. I lost 
of course ;) IIRC, his argument was that it would require perfect flow 
analysis and that's too difficult and expensive. My argument was that it 
didn't need to be, and perhaps even *shouldn't* be, perfect. He felt that 
would end up being a PITA with false errors, and I felt that C# demonstrates 
it isn't a PITA.  Meh, I don't want to re-debate it though.

>> I think these features probably belong to some lint-type tool and not
>> the compiler.
>
> If I remember correctly, one of the goals of D according to
> Walter is to reduce the need for such external tools.

I've argued in the past that warnings *are* a lint tool. The powers that be 
didn't seem to agree though.




More information about the Digitalmars-d mailing list