Project

General

Profile

Support #692

Local variables hiding out ones at top level

Added by Anna Maria Bigatti almost 9 years ago. Updated over 3 years ago.

Status:
Rejected
Priority:
Normal
Assignee:
Category:
Parser/Interpreter
Target version:
Start date:
04 May 2015
Due date:
% Done:

100%

Estimated time:
0.99 h
Spent time:

Description

In function definitions local variables hiding outer ones are signalled by a Warning.
At top level not. This might be confusing!
(... when someone has the bad habit of calling all variables x ;-)

/**/ Use QQ[x,y,z];

/**/ for x:=1 to 6 do print x+1 ; endfor;
234567

/**/ [ x+5 | x in 1..6];
[6, 7, 8, 9, 10, 11]


Related issues

Related to CoCoA-5 - Bug #486: Warning about variable hiding another -- too zealousNew2014-03-20

History

#1 Updated by John Abbott almost 9 years ago

In the example given I note that x is actually a protected variable -- perhaps even more reason for a warning?

#2 Updated by John Abbott about 4 years ago

  • Target version changed from CoCoA-5.?.? to CoCoA-5.4.0

I'm now quite undecided about this.

Every so often I do get a warning about one variable hiding another, but it is almost always a nuisance rather than a help. To eliminate the warning I must then change the name of a variable, but that can be "annoying".

Here is an artificial example:

define fn(L)
  for j:=1 to len(L) do something; endfor;
  j := 1; while not(IsGood(L[j]) do incr(ref j); endwhile;
  // now use index j ...
enddefine;

#3 Updated by John Abbott over 3 years ago

  • Status changed from New to In Progress
  • % Done changed from 0 to 10

I think I now prefer not to issue a warning in the examples given here.
If CoCoA issues too many warnings then the user may end up simply ignoring them.
As hinted in the comment above, I might even be tempted to remove the warnings which are currently given inside a user defined function when a local variable is also used as a loop variable. Here is another example of an annoying warning:

define john(n)
  L := [i in 1..n | IsPrime(i)];
  i := 1;
  while i <= len(L) do
    if IsPrime(L[i]+2) then return i; endif;
  endwhile;
  return 0;
enddefine;

The main point here is that he loop variable i in the first line of the function appears before the "scope" of the local variable i begins. However, CoCoA extends the "scope" of the local variable to the whole function... so a warning is issued.
They do say that using the same variable for different purposes inside a single function is poor style... :-/

#4 Updated by John Abbott over 3 years ago

  • Status changed from In Progress to Rejected
  • Assignee set to John Abbott
  • % Done changed from 10 to 100
  • Estimated time set to 0.99 h

After a Skype discussion we have decided to leave things as they are: while not perfect (and sometimes annoying) the warnings may be a useful guide to programmers with not so much experience.

Also available in: Atom PDF