Can variables be declared within a block?

classic Classic list List threaded Threaded
31 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Can variables be declared within a block?

vfclists .

Can variables be declared within a begin end block?
eg.
if y = xxx then
begin
  var xxx;
  var yyy;
end

There are there as shortnames for  long expressions

--
Frank Church

=======================
http://devblog.brahmancreations.com

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Can variables be declared within a block?

Michael Van Canneyt


On Thu, 14 Oct 2010, Frank Church wrote:

> Can variables be declared within a begin end block?

No.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Can variables be declared within a block?

zaher dirkey


On Thu, Oct 14, 2010 at 11:21 AM, Michael Van Canneyt <[hidden email]> wrote:


On Thu, 14 Oct 2010, Frank Church wrote:

Can variables be declared within a begin end block?

No.

That found in Gnu Pascal
Can we ask to be feature request?

Thanks
--
Zaher Dirkey

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Can variables be declared within a block?

Florian Klaempfl
Am 18.10.2010 08:17, schrieb Zaher Dirkey:

>
>
> On Thu, Oct 14, 2010 at 11:21 AM, Michael Van Canneyt
> <[hidden email] <mailto:[hidden email]>> wrote:
>
>
>
>     On Thu, 14 Oct 2010, Frank Church wrote:
>
>         Can variables be declared within a begin end block?
>
>
>     No.
>
>
> That found in Gnu Pascal
> Can we ask to be feature request?
>

No.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Can variables be declared within a block?

vfclists .


On 18 October 2010 07:49, Florian Klaempfl <[hidden email]> wrote:
Am 18.10.2010 08:17, schrieb Zaher Dirkey:
>
>
> On Thu, Oct 14, 2010 at 11:21 AM, Michael Van Canneyt
> <[hidden email] <mailto:[hidden email]>> wrote:
>
>
>
>     On Thu, 14 Oct 2010, Frank Church wrote:
>
>         Can variables be declared within a begin end block?
>
>
>     No.
>

That is a rather emphatic no :).
 Is there some performance or other related reason why it is a bad idea? I'm not knowledgeable in this area but I'd like to know.

Sometimes my variable declarations is filled with lots of variables for the sake of making code involving long derived expressions comprehensible and the clutter the understanding at the top level of the procedure.

Can a procedure be declared inline just for the sake of introducing those variables?

 
>
> That found in Gnu Pascal
> Can we ask to be feature request?
>

No.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal



--
Frank Church

=======================
http://devblog.brahmancreations.com

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Can variables be declared within a block?

el_es
On 18/10/2010 13:58, Frank Church wrote:
> On 18 October 2010 07:49, Florian Klaempfl <[hidden email]> wrote:
[...]

>>>     No.
>>>
>>
>
> That is a rather emphatic no :).
>  Is there some performance or other related reason why it is a bad idea? I'm
> not knowledgeable in this area but I'd like to know.
>
> Sometimes my variable declarations is filled with lots of variables for the
> sake of making code involving long derived expressions comprehensible and
> the clutter the understanding at the top level of the procedure.
>
> Can a procedure be declared inline just for the sake of introducing those
> variables?
>
>
I guess it has something to do with 'functions/procedures not to be longer than
3 editor screens' and 'if it is longer and has more responsibilities than
the name implies, do split it up' kind of paradigm.
c(-like) languages are very bad (don't keep you restrained) at this kind of use ;)
and Gnu Pascal is a mere frontend to GCC so it may inherit c-isms ;)

Having variables declared within code block used to require to parse the source code
at least 2 times as opposed to nice Pascal way of doing it ONCE only. This was a compiler
performance winner in the times of ancient 286, 386 ;)
(dug out from depths of my memory, may be outdated badly).

L.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Can variables be declared within a block?

Florian Klaempfl
In reply to this post by vfclists .
Am 18.10.2010 14:58, schrieb Frank Church:

>
>
> On 18 October 2010 07:49, Florian Klaempfl <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     Am 18.10.2010 08:17, schrieb Zaher Dirkey:
>     >
>     >
>     > On Thu, Oct 14, 2010 at 11:21 AM, Michael Van Canneyt
>     > <[hidden email] <mailto:[hidden email]>
>     <mailto:[hidden email] <mailto:[hidden email]>>> wrote:
>     >
>     >
>     >
>     >     On Thu, 14 Oct 2010, Frank Church wrote:
>     >
>     >         Can variables be declared within a begin end block?
>     >
>     >
>     >     No.
>     >
>
>
> That is a rather emphatic no :).
>  Is there some performance or other related reason why it is a bad idea?

It's just a matter of readability, e.g. having a variable declared in
two blocks with the same name can cause you a lot of headache:

procedure p;

begin
  ...
  begin
    var i : integer;
    ...
    ...
    ...
    i:=12341234;
    ...
    ...
    ...
    ...
  end;
  ...
  ...
  ...
  begin
    var i : integer;
    ...
    writeln(i); <--- this i is not the same as above so i does not
contain 12341234 but it contains probably some garbage.
  end;
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Can variables be declared within a block?

Bernd Kreuss
In reply to this post by el_es
On 18.10.2010 15:19, Lukasz Sokol wrote:

> Having variables declared within code block used to require to parse the source code
> at least 2 times

Or create them on the stack at the begin and remove them at the end of
the block (which would be the only way to have some additional
functionality through this).

Bernd
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Can variables be declared within a block?

vfclists .
In reply to this post by Florian Klaempfl


On 18 October 2010 14:42, Florian Klaempfl <[hidden email]> wrote:
Am 18.10.2010 14:58, schrieb Frank Church:
>
>
> On 18 October 2010 07:49, Florian Klaempfl <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     Am 18.10.2010 08:17, schrieb Zaher Dirkey:
>     >
>     >
>     > On Thu, Oct 14, 2010 at 11:21 AM, Michael Van Canneyt
>     > <[hidden email] <mailto:[hidden email]>
>     <mailto:[hidden email] <mailto:[hidden email]>>> wrote:
>     >
>     >
>     >
>     >     On Thu, 14 Oct 2010, Frank Church wrote:
>     >
>     >         Can variables be declared within a begin end block?
>     >
>     >
>     >     No.
>     >
>
>
> That is a rather emphatic no :).
>  Is there some performance or other related reason why it is a bad idea?

It's just a matter of readability, e.g. having a variable declared in
two blocks with the same name can cause you a lot of headache:

procedure p;

begin
 ...
 begin
   var i : integer;
   ...
   ...
   ...
   i:=12341234;
   ...
   ...
   ...
   ...
 end;
 ...
 ...
 ...
 begin
   var i : integer;
   ...
   writeln(i); <--- this i is not the same as above so i does not
contain 12341234 but it contains probably some garbage.
 end;


My need is not for such kind or variables - it is for something like this.

say I have some thing like:
 StringList.NameOfIndex(TableName.FieldByName('xxxx').AsString) > AnotherEqualLongExpression + SomeOtherEvenLongerExpression.

I just want to create shorter variables like
si :=  StringList.NameOfIndex(TableName.FieldByName('xxxx').AsString);
ae := xxx;
so:= yyy

to express complicated logic with them.

When I put them all at the top of the procedure, they are extra noise, which is not necessary for the understanding the purpose at the top level of the procedure.

I understand the need to avoid stuff that could create unintended errors and have experienced such issues before. I will be careful not to use the more than enough rope I have been given to hang myself. I promise :)
 
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal



--
Frank Church

=======================
http://devblog.brahmancreations.com

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Can variables be declared within a block?

vfclists .


On 18 October 2010 14:55, Frank Church <[hidden email]> wrote:


On 18 October 2010 14:42, Florian Klaempfl <[hidden email]> wrote:
Am 18.10.2010 14:58, schrieb Frank Church:
>
>
> On 18 October 2010 07:49, Florian Klaempfl <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     Am 18.10.2010 08:17, schrieb Zaher Dirkey:
>     >
>     >
>     > On Thu, Oct 14, 2010 at 11:21 AM, Michael Van Canneyt
>     > <[hidden email] <mailto:[hidden email]>
>     <mailto:[hidden email] <mailto:[hidden email]>>> wrote:
>     >
>     >
>     >
>     >     On Thu, 14 Oct 2010, Frank Church wrote:
>     >
>     >         Can variables be declared within a begin end block?
>     >
>     >
>     >     No.
>     >
>
>
> That is a rather emphatic no :).
>  Is there some performance or other related reason why it is a bad idea?

It's just a matter of readability, e.g. having a variable declared in
two blocks with the same name can cause you a lot of headache:

procedure p;

begin
 ...
 begin
   var i : integer;
   ...
   ...
   ...
   i:=12341234;
   ...
   ...
   ...
   ...
 end;
 ...
 ...
 ...
 begin
   var i : integer;
   ...
   writeln(i); <--- this i is not the same as above so i does not
contain 12341234 but it contains probably some garbage.
 end;


My need is not for such kind or variables - it is for something like this.

say I have some thing like:
 StringList.NameOfIndex(TableName.FieldByName('xxxx').AsString) > AnotherEqualLongExpression + SomeOtherEvenLongerExpression.

I just want to create shorter variables like
si :=  StringList.NameOfIndex(TableName.FieldByName('xxxx').AsString);
ae := xxx;
so:= yyy

to express complicated logic with them.

When I put them all at the top of the procedure, they are extra noise, which is not necessary for the understanding the purpose at the top level of the procedure.

I understand the need to avoid stuff that could create unintended errors and have experienced such issues before. I will be careful not to use the more than enough rope I have been given to hang myself. I promise :)
 

I sometimes use nested procedures for such purposes when it comes to the program logic itself, having something like that for variables would be nice.

PS. The promise above should be "we promise" :)
 
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal



--
Frank Church

=======================
http://devblog.brahmancreations.com



--
Frank Church

=======================
http://devblog.brahmancreations.com

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Can variables be declared within a block?

Bernd Kreuss
In reply to this post by vfclists .
On 18.10.2010 15:55, Frank Church wrote:

> I just want to create shorter variables like
> si :=  StringList.NameOfIndex(TableName.FieldByName('xxxx').AsString);
> ae := xxx;
> so:= yyy
>
> to express complicated logic with them.

Maybe the usage of WITH could be helpful in some of these cases

Bernd
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Can variables be declared within a block?

Bernd Kreuss
In reply to this post by vfclists .
On 18.10.2010 15:55, Frank Church wrote:

> I understand the need to avoid stuff that could create unintended errors

It would create something that I would call "irregularity" (or even
ugliness) in the language grammar. Nowhere else is it ever allowed to
have certain things inside a begin/end block, type, const, var are all
only allowed in the *same* certain places outside of such blocks.
Allowing var would also raise the question why not also allow type and
const and this would open a can of worms. It would destroy some (much)
of the simplicity (regularity, strictness, beauty, I don't know a better
word) of the grammar.

Bernd
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Can variables be declared within a block?

Michael Van Canneyt


On Mon, 18 Oct 2010, Bernd Kreuss wrote:

> On 18.10.2010 15:55, Frank Church wrote:
>
>> I understand the need to avoid stuff that could create unintended errors
>
> It would create something that I would call "irregularity" (or even
> ugliness) in the language grammar. Nowhere else is it ever allowed to
> have certain things inside a begin/end block, type, const, var are all
> only allowed in the *same* certain places outside of such blocks.
> Allowing var would also raise the question why not also allow type and
> const and this would open a can of worms. It would destroy some (much)
> of the simplicity (regularity, strictness, beauty, I don't know a better
> word) of the grammar.

My thoughts exactly.

Object Pascal is a simple, beautiful and easy to read language.
The FPC team tries to keep it that way.

If someone needs convincing that this is actually a plus:
go and (try to) read and understand any moderately complicated perl program.
Latex2html is a nice start :-)

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Can variables be declared within a block?

Benedikt Schindler
In reply to this post by vfclists .

>
>     I just want to create shorter variables like
>     si :=  StringList.NameOfIndex(TableName.FieldByName('xxxx').AsString);
>     ae := xxx;
>     so:= yyy
>
>     to express complicated logic with them.
>
>     When I put them all at the top of the procedure, they are extra
>     noise, which is not necessary for the understanding the purpose at
>     the top level of the procedure.

I don't see the great extra noise of a

procedure SomeProc([someVars : Type]);
var
  [somevars : type;]
  si,ae,so : double; // temp vars for easy math;
begin
  [...]
end;

and i think the easy answer is:

No you could't, because in pascal you have to declare the vars in the
beginning of a procedure.

or:

No because this is pascal and not C

or:

why couldn't i use spanish in my english? :) *just kidding*


have all a nice day.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Can variables be declared within a block?

Sven Barth-2
In reply to this post by Michael Van Canneyt
Am 18.10.2010 16:20, schrieb Michael Van Canneyt:
> Object Pascal is a simple, beautiful and easy to read language. The FPC
> team tries to keep it that way.

And that is why I like the FPC team so much. :D

Regards,
Sven
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Can variables be declared within a block?

David W Noon
In reply to this post by Florian Klaempfl
On Mon, 18 Oct 2010 15:42:31 +0200, Florian Klaempfl wrote about Re:
[fpc-pascal] Can variables be declared within a block?:

[snip]

> It's just a matter of readability, e.g. having a variable declared in
> two blocks with the same name can cause you a lot of headache:
>
> procedure p;
>
> begin
>   ...
>   begin
>     var i : integer;
>     ...
>     ...
>     ...
>     i:=12341234;
>     ...
>     ...
>     ...
>     ...
>   end;
>   ...
>   ...
>   ...
>   begin
>     var i : integer;
>     ...
>     writeln(i); <--- this i is not the same as above so i does not
> contain 12341234 but it contains probably some garbage.
>   end;
This was not usually a problem in ALGOL 60/68, PL/I or C/C++ -- these
languages have supported short scopes for up to 50 years -- as the
programmer was required to be aware of the different scopes of the
like-named variables.

I still use short scoped variables today, usually in C or C++.  I have
never had any such problems, even going back almost 40 years when I
programmed in ALGOL 60.  All block structured languages require the
programmer to understand the scope of all variables.

Indeed, short scopes can often circumvent name collisions, e.g.

 long some_func(unsigned long arg_1)
 {
    long i;  /* procedure/function scoped */
    unsigned max_iters;
    . . .
    /* calculate "i", "max_iters" and any others */
    . . .
    for (unsigned i = 0; i < max_iters; ++i) /* short scope "i" */
    {
       /* "i" here is distinct from the one at function scope. */
       . . .
    }
    . . .
    return i; /* back to procedure scoped "i". */
 }

Whether it is wise to use such lax naming of variables is open to
debate, but at least C and C++ allow the above.

That said, short scoping was never a part of original Pascal.  I think
Jensen & Wirth omitted it because it made the compiler simpler.
--
Regards,

Dave  [RLU #314465]
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[hidden email] (David W Noon)
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

signature.asc (205 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Can variables be declared within a block?

Vannus
In reply to this post by Sven Barth-2

On 18 October 2010 15:33, Sven Barth <[hidden email]> wrote:
Am 18.10.2010 16:20, schrieb Michael Van Canneyt:

Object Pascal is a simple, beautiful and easy to read language. The FPC
team tries to keep it that way.

And that is why I like the FPC team so much. :D


agreed.

When I went from C++ to Pascal back in 98/99, I found it quite hard initially. Not being able to put variables anywhere & everywhere was really awkward, but it dawned on me that I shouold be coding better.
Since then, i would say "If you need vars throughout your code, then something is wrong with your code"

- V

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Can variables be declared within a block?

Reimar Grabowski
On Mon, 18 Oct 2010 19:21:57 +0100
Vannus <[hidden email]> wrote:

> Since then, i would say "If you need vars throughout your code, then
> something is wrong with your code"
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?

R.
--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing in e-mail?
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Can variables be declared within a block?

Andreas Berger

> 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?
>
> R.
But it should make you wonder why this is no longer allowed in the
latest C++ standards.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Can variables be declared within a block?

David W Noon
On Mon, 18 Oct 2010 19:46:04 -0200, Andreas Berger wrote about Re:
[fpc-pascal] Can variables be declared within a block?:

>
> > 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?
> >
> > R.
> But it should make you wonder why this is no longer allowed in the
> latest C++ standards.
What do you mean?

The current C++ standard (ISO 1998+TR1) permits short scope
declarations.  Indeed, C++ permits declarations to appear anywhere, not
just after a { to open a function or compound statement.  It has done
since the A.R.M. days, and in some implementations before that.
--
Regards,

Dave  [RLU #314465]
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[hidden email] (David W Noon)
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

signature.asc (205 bytes) Download Attachment
12