variables, assignments, and arithmetic

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

variables, assignments, and arithmetic

JordanBrown

I understand that OpenSCAD's variables are ... unusual.

But I'm puzzled by this:

n = 0;
p = 0;

n = p;
echo(n=n, p=p);

which yields:

WARNING: Ignoring unknown variable 'p'. 

ECHO: n = undef, p = 0

I'm not coming up with a mental model that explains this behavior.


Can somebody briefly describe what's really going on?



  


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

Re: variables, assignments, and arithmetic

tp3
On 08/02/2017 08:31 PM, Jordan Brown wrote:
> Can somebody briefly describe what's really going on?
>
The reassignment is placed at the point of the original variable,
so the evaluation sees:

n = p;
p = 0;
echo(n=n, p=p);

ciao,
  Torsten.

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

Re: variables, assignments, and arithmetic

frankv
Variables in OpenSCAD aren't really variables. They are constants to which you can assign a value. You can have multiple assignments to a variable, but only the last one sets the value. 

On 3/08/2017 06:37, "Torsten Paul" <[hidden email]> wrote:
On 08/02/2017 08:31 PM, Jordan Brown wrote:
> Can somebody briefly describe what's really going on?
>
The reassignment is placed at the point of the original variable,
so the evaluation sees:

n = p;
p = 0;
echo(n=n, p=p);

ciao,
  Torsten.

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

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

Re: variables, assignments, and arithmetic

JordanBrown
In reply to this post by tp3
On 8/2/2017 11:36 AM, Torsten Paul wrote:
On 08/02/2017 08:31 PM, Jordan Brown wrote:
Can somebody briefly describe what's really going on?

The reassignment is placed at the point of the original variable,
so the evaluation sees:

n = p;
p = 0;
echo(n=n, p=p);


Thanks.  Makes sense... well, sort of :-)

On 8/2/2017 4:37 PM, Frank van der Hulst wrote:
Variables in OpenSCAD aren't really variables. They are constants to which you can assign a value. You can have multiple assignments to a variable, but only the last one sets the value.

Right.  The question was why, in the example I originally quoted
n = 0;
p = 0;

n = p;

I got an unknown variable error.  I was confused since, looking at the lines above in the sequence above, at the point where "p" is used it's been defined.


  


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

Re: variables, assignments, and arithmetic

MichaelAtOz
Administrator
n is instantiated on the first line, when p is not defined.
n is then redefined as p it looks for p in the instantiated state, p does not exist.

Compare to:

p = undef;
n = 0;
p = 0;

n = p;
echo(n=n,p=p);

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!
Loading...