[fpc-pascal] Can variables be declared within a block?

Vannus vannus at gmail.com
Tue Oct 19 22:18:03 CEST 2010


On 19 October 2010 15:06, Rob Kennedy
<kennedyri+fpc-pascal at gmail.com<kennedyri%2Bfpc-pascal at gmail.com>
> wrote:

> On Tue, Oct 19, 2010 at 12:23 AM, Jürgen Hestermann <
> juergen.hestermann at gmx.de> wrote:
>
>> Reimar Grabowski schrieb:
>>
>>  for (int i = 0;...)
>>> Can't see anything wrong. I use declaration of variables inside blocks
>>> quite often in Java and C++ but have never missed it in pascal. Please
>>> enlighten me. What is so bad about creating temporary variables inside
>>> blocks instead of the beginning of a function in a language that supports
>>> it?
>>>
>>
your example is at the start of a "for loop" block, which is much the same
as having it at the start of a procedure block

pascal prevents people from putting declarations strewn throughout the
block, which i found reduces mistakes & bugs from 20-30 people coding, and
their code is more readable. Coding standards are do similar thing.

which of the three languages do you find fastest to code in, including any
debugging time?


>
>> I never thought about doing such declarations in Pascal.
>> Although I would not be against it in general (provided
>> that all identifiers have to be different)
>
>
> That seems like an arbitrary restriction, given that Object Pascal
> *already* allows multiple variables to have the same name *and* be declared
> somewhere other than the top of the function. Think of exception handlers. I
> don't know about everyone else, but my exception variables are all always
> named e.
>

are you sure? i've tried making var's in WITh and Try Except blocks using
Delphi7 with no luck. Or is this a difference between object pascal &
delphi?


>
> I could understand disallowing variables from *shadowing* other local
> variables of the same name (it's legal in C, but better compilers warn about
> it), but I see no reason for the language to require variables from
> different local scopes to have unique names. Write that into your local
> coding guidelines and enforce it in code reviews, not the language spec.
>
>
i'd agree with that, local vars are local after all. perhaps make it a
compiler option.

- V
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20101019/3f7812d6/attachment.html>


More information about the fpc-pascal mailing list