echo function (maybe red herring) and scope

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

echo function (maybe red herring) and scope

MichaelAtOz
Administrator
I'm producing some examples of the echo function.

I came across a funny scope thing.

> z=0;
> //i=3;
> j=4;
> k=5;
> i=3;   // comment out & uncomment line 2
>
> i = echo(j=j,k=k) z; //  set new value of i to z (ie 0)
> echo(i=i,j=j);
>
> o= 10 + echo(i=i) 5;
> echo(o=o); // 15

That works as expected.
If you invert the commented out lines it produces

> WARNING: Ignoring unknown variable 'j'.
> WARNING: Ignoring unknown variable 'k'.
> ECHO: j = undef, k = undef
> ECHO: i = 0
> ECHO: i = 0, j = 4
> ECHO: o = 15

I can understand the strange scoping behaviour if j or k was dependent on i,
buy i is the target of the assignment, I can't see why this happens.

Bug?



-----
Admin - PM me if you need anything, or if I've done something stupid...

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above.

The TPP is no simple “trade agreement.”   Fight it! http://www.ourfairdeal.org/   time is running out!
--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Admin - email* me if you need anything,
or if I've done something stupid...
* click on my MichaelAtOz label, there is a link to email me.

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work.
Obviously inclusion of works of previous authors is not included in the above.


The TPP is no simple “trade agreement.” Fight it! http://www.ourfairdeal.org/ time is running out!
Reply | Threaded
Open this post in threaded view
|

Re: echo function (maybe red herring) and scope

MichaelAtOz
Administrator
Also, I'm going to do the wiki doco for echo, unless someone has that
squirreled away somewhere??



-----
Admin - PM me if you need anything, or if I've done something stupid...

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above.

The TPP is no simple “trade agreement.”   Fight it! http://www.ourfairdeal.org/   time is running out!
--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Admin - email* me if you need anything,
or if I've done something stupid...
* click on my MichaelAtOz label, there is a link to email me.

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work.
Obviously inclusion of works of previous authors is not included in the above.


The TPP is no simple “trade agreement.” Fight it! http://www.ourfairdeal.org/ time is running out!
Reply | Threaded
Open this post in threaded view
|

Re: echo function (maybe red herring) and scope

kintel
Administrator
In reply to this post by MichaelAtOz
Hi Michael,

I offered this explanation recently, perhaps that helps you understand this off quirk as well:

* All assignments in a scope is hoisted to the top of that scope (i.e. all assignment are performed before any other statements)
* All assignments are performed in their declared order
* If an assignment is made twice, the second assignment takes precedence, but the order does not change.

Something like this probably belongs in the FAQ; just don’t know how to best phase it in a way FAQ readers would benefit from.

 -Marius


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: echo function (maybe red herring) and scope

MichaelAtOz
Administrator
So in this case (with line 2 un-commented) , the echo() fn is evaluated at
scope of line two, when j & k are undef. OK. I tried to document this in the
wiki some time ago, I'll see if I can integrate your phraseology.



-----
Admin - PM me if you need anything, or if I've done something stupid...

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above.

The TPP is no simple “trade agreement.”   Fight it! http://www.ourfairdeal.org/   time is running out!
--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Admin - email* me if you need anything,
or if I've done something stupid...
* click on my MichaelAtOz label, there is a link to email me.

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work.
Obviously inclusion of works of previous authors is not included in the above.


The TPP is no simple “trade agreement.” Fight it! http://www.ourfairdeal.org/ time is running out!
Reply | Threaded
Open this post in threaded view
|

Re: echo function (maybe red herring) and scope

kintel
Administrator
> On Feb 24, 2018, at 11:26 PM, MichaelAtOz <[hidden email]> wrote:
>
> So in this case (with line 2 un-commented) , the echo() fn is evaluated at
> scope of line two, when j & k are undef.

Exactly.

 -Marius


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: echo function (maybe red herring) and scope

Ronaldo
In reply to this post by kintel
This explanation is of little help when the issue arises in a long code with many used libraries because the warning gives no clues where the problem is. The poor fellow who gets the warning should scan all the code, even of used libraries, before the offending assignment looking for another assignment of the same variable. It is an error very hard to debug even for experienced users.

2018-02-25 1:16 GMT-03:00 Marius Kintel <[hidden email]>:
I offered this explanation recently, perhaps that helps you understand this off quirk as well:

* All assignments in a scope is hoisted to the top of that scope (i.e. all assignment are performed before any other statements)
* All assignments are performed in their declared order
* If an assignment is made twice, the second assignment takes precedence, but the order does not change.

Something like this probably belongs in the FAQ; just don’t know how to best phase it in a way FAQ readers would benefit from.

 -Marius


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org