Re: Changing elements of "global" structure/vector in function

classic Classic list List threaded Threaded
3 messages Options
Samuel GOUGEON Samuel GOUGEON
Reply | Threaded
Open this post in threaded view
|

Re: Changing elements of "global" structure/vector in function

Le 29/11/2012 12:46, Lamy Alain a écrit :

> Changing elements of a "global" (i.e. visible) variable in a function may be dangerous.
> A new structure or vector is created that only contains the elements that have been changed.
>
> I would expect the whole structure/vector to be copied locally before changing part of them.
>
> A workaround is to create a local copy in the function by an instruction like:
> A=A
> (see fun3)
> which looks a bit strange at first sight as it seems to do nothing.
>
> Has anyone an opinion on whether this is considered as correct behaviour or not ?
> Or is it a bug ?
AFAIU, i do not think that it is a bug.
When a LHS symbol is not yet defined, then Scilab creates it as a recipient.
It is the usual behavior. While if a RHS symbol is not yet defined,
the expected source is missing, and rather than yielding an error, Scilab
tries to look for it outside. Then, the "global"  link is set.

So, this could look tricky, but yes: A=A does it ;-)

Samuel
_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Alain LAMY Alain LAMY
Reply | Threaded
Open this post in threaded view
|

Re: Changing elements of "global" structure/vector in function

I understand the logic.
 
My concern was that it is easy to make a mistake
(we write A(5)=1, thinking that the other elements are unchanged and in fact
everything else is set to 0).
Arguments to function behave differently (but they are considered as local variables).

I guess one way could be to add a kind of declaration keywork like:
"import VAR"
whose effect would be to copy the "VAR" variable locally.
We could even imagine that all variables in a function should either be local are declared
in some way (some kind of "strict" scilab mode as it exists in perl).

Maybe a suggestion for future scilab versions ?
       

-----Message d'origine-----
De : [hidden email] [mailto:[hidden email]] De la part de Samuel Gougeon
Envoyé : jeudi 29 novembre 2012 22:20
À : International users mailing list for Scilab.
Objet : Re: [Scilab-users] Changing elements of "global" structure/vector in function

Le 29/11/2012 12:46, Lamy Alain a écrit :

> Changing elements of a "global" (i.e. visible) variable in a function may be dangerous.
> A new structure or vector is created that only contains the elements that have been changed.
>
> I would expect the whole structure/vector to be copied locally before changing part of them.
>
> A workaround is to create a local copy in the function by an instruction like:
> A=A
> (see fun3)
> which looks a bit strange at first sight as it seems to do nothing.
>
> Has anyone an opinion on whether this is considered as correct behaviour or not ?
> Or is it a bug ?
AFAIU, i do not think that it is a bug.
When a LHS symbol is not yet defined, then Scilab creates it as a recipient.
It is the usual behavior. While if a RHS symbol is not yet defined,
the expected source is missing, and rather than yielding an error, Scilab
tries to look for it outside. Then, the "global"  link is set.

So, this could look tricky, but yes: A=A does it ;-)

Samuel
_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Alain LAMY Alain LAMY
Reply | Threaded
Open this post in threaded view
|

Re: Changing elements of "global" structure/vector in function

In reply to this post by Samuel GOUGEON
Correction of typo in my previous answer:

... that all variables in a function should either be local OR declared
in some way ...

(the initial text may have been difficult to understand)




_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users