Read-only forum archive

redefine and kill in for loops

redefine and kill in for loops

Jack · Thu Oct 10, 2013 12:41 pm

Consider a list l of rings or other objects.

Code:
...
def S;
int pos;
for (pos=1; pos<=size(l); pos++)
{
  s=l[pos];
}


Is it necessary to kill and redefine S in each loop step?

Re: redefine and kill in for loops

hannes · Fri Oct 11, 2013 11:38 am

If you want to assign l[pos] to s then the types must allow this.
For example, if s is of type int and l[pos] is of type string,
the assignment is not possible.
But if s will destroyed first and redefined as of type def
the assignment works and the type of s (which started as type def)
changes to string.