discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

echo function (maybe red herring) and scope

M
MichaelAtOz
Sun, Feb 25, 2018 3:22 AM

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/

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/
M
MichaelAtOz
Sun, Feb 25, 2018 3:24 AM

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/

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/
MK
Marius Kintel
Sun, Feb 25, 2018 4:16 AM

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

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
M
MichaelAtOz
Sun, Feb 25, 2018 4:26 AM

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/

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/
MK
Marius Kintel
Sun, Feb 25, 2018 4:32 AM

On Feb 24, 2018, at 11:26 PM, MichaelAtOz oz.at.michael@gmail.com 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

> On Feb 24, 2018, at 11:26 PM, MichaelAtOz <oz.at.michael@gmail.com> 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
RP
Ronaldo Persiano
Sun, Feb 25, 2018 1:25 PM

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 marius@kintel.net:

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

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 <marius@kintel.net>: > 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 > >