Puzzled here

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

Puzzled here

Experimentalist
Hi

I am sure I must be doing something very silly here but I just can't spot it. Here is some test code to demonstrate my issue:

>>>
Test(choice = 1);
// Test(choice = 2);
// Test(choice = 3);

module Test(choice = 1){
    echo(choice=choice);
    myVar = 0;
   
    if (choice==1) {
        myVar = 1;
    } else if (choice == 2) {
        myVar = 2;
    } else {
         myVar = 3;
    }

    echo(myVar=myVar);
}
<<<

Why is myVar always 0 at the echo statement?

Hope you can help
Ex

Reply | Threaded
Open this post in threaded view
|

Re: Puzzled here

doug.moen
OpenSCAD is a declarative language. It doesn't have mutable variables (or mutable data structures). Each variable is defined in exactly one location. It does have nested scopes, and {...} introduces a new scope, which is why you don't get an error.

You can't use multiple assignment statements to specify the value of a variable.

Try this:
 myVar =
    (choice==1) ?
        1
    : (choice == 2) ?
        2
    :
         3;
 

On 23 April 2016 at 11:13, Experimentalist <[hidden email]> wrote:
Hi

I am sure I must be doing something very silly here but I just can't spot
it. Here is some test code to demonstrate my issue:

>>>
Test(choice = 1);
// Test(choice = 2);
// Test(choice = 3);

module Test(choice = 1){
    echo(choice=choice);
    myVar = 0;

    if (choice==1) {
        myVar = 1;
    } else if (choice == 2) {
        myVar = 2;
    } else {
         myVar = 3;
    }

    echo(myVar=myVar);
}
<<<

Why is myVar always 0 at the echo statement?

Hope you can help
Ex





--
View this message in context: http://forum.openscad.org/Puzzled-here-tp17172.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

_______________________________________________
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
|

Re: Puzzled here

wolf
You can reassign values to variables. This shows you how:
Test(choice = 1);
// Test(choice = 2);
// Test(choice = 3);

module Test(choice = 1){
    echo(choice=choice);
 //   myVar = 0;
   
    if (choice==1) {
        myVar=var(choice);
    } else if (choice == 2) {
        myVar=var(choice);
    } else {
         myVar=var(choice);
    }

    echo(myVar);
}
function var(choice)=choice;


wolf
Reply | Threaded
Open this post in threaded view
|

Re: Puzzled here

nophead
ECHO: choice = 1

WARNING: Ignoring unknown variable 'myVar'.

ECHO: undef


What makes you think it works? Each myVar is a different variable because brackets open a new scope. None of them are in scope when it gets to the echo.


On 23 April 2016 at 22:34, wolf <[hidden email]> wrote:
You can reassign values to variables. This shows you how:
Test(choice = 1);
// Test(choice = 2);
// Test(choice = 3);

module Test(choice = 1){
    echo(choice=choice);
 //   myVar = 0;

    if (choice==1) {
        myVar=var(choice);
    } else if (choice == 2) {
        myVar=var(choice);
    } else {
         myVar=var(choice);
    }

    echo(myVar);
}
function var(choice)=choice;


wolf



--
View this message in context: http://forum.openscad.org/Puzzled-here-tp17172p17176.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

_______________________________________________
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
|

Re: Puzzled here

doug.moen
In reply to this post by wolf
Wolf, your program does not work, and contains errors. Here's the console output that I get:

ECHO: choice = 1

WARNING: Ignoring unknown variable 'myVar'.

ECHO: undef


On 23 April 2016 at 17:34, wolf <[hidden email]> wrote:
You can reassign values to variables. This shows you how:
Test(choice = 1);
// Test(choice = 2);
// Test(choice = 3);

module Test(choice = 1){
    echo(choice=choice);
 //   myVar = 0;

    if (choice==1) {
        myVar=var(choice);
    } else if (choice == 2) {
        myVar=var(choice);
    } else {
         myVar=var(choice);
    }

    echo(myVar);
}
function var(choice)=choice;


wolf



--
View this message in context: http://forum.openscad.org/Puzzled-here-tp17172p17176.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

_______________________________________________
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
|

Re: Puzzled here

wolf
Sorry, for a moment I did not remember that a module is intended to return a shape, and a function to return a value. Thus, the confusion. What I should have sent is this:

choice = 1;

echo(myVar());
Test(myVar());

module Test(xyz)
  {
    if       (xyz==1)      cube([10,xyz,xyz]);
    else if  (xyz == 2)    cube([xyz,10,xyz]);
    else if  (xyz == 3)    cube([xyz,xyz,10]);
    else                   sphere(10);
  }

function myVar()=choice==1?1:
                 choice==2?2:
                 choice==3?3:"not in list";

By changing the value assigned to choice, you may change the value reported by echo() and the shape returned by Test(), which is what -I think- experimentalist originally intended to do, when we were all mislead by doug.moen's correct, but misleading answer.
I have read similar questions on these pages before, so the issue is not new. To overcome it, may I suggest that above sample program be added to the manual so that others in future can look up the answer there? In particular, try to stress that module returns a shape, function a value!

wolf
Reply | Threaded
Open this post in threaded view
|

Re: Puzzled here

Experimentalist
"what -I think- experimentalist originally intended to do"

What I was trying to do was change the Z offset of a translation in a module by passing a more concise parameter to avoid code duplication and improve readability. With your help I have added a function and achieved my goal, thanks.

I never have, so maybe it is time to go RTFM.
Reply | Threaded
Open this post in threaded view
|

Re: Puzzled here

L Boyd
In reply to this post by wolf
wolf wrote
Sorry, for a moment I did not remember that a module is intended to return a shape, and a function to return a value. Thus, the confusion. What I should have sent is this:

choice = 1;

echo(myVar());
Test(myVar());

module Test(xyz)
  {
    if       (xyz==1)      cube([10,xyz,xyz]);
    else if  (xyz == 2)    cube([xyz,10,xyz]);
    else if  (xyz == 3)    cube([xyz,xyz,10]);
    else                   sphere(10);
  }

function myVar()=choice==1?1:
                 choice==2?2:
                 choice==3?3:"not in list";

By changing the value assigned to choice, you may change the value reported by echo() and the shape returned by Test(), which is what -I think- experimentalist originally intended to do, when we were all mislead by doug.moen's correct, but misleading answer.
I have read similar questions on these pages before, so the issue is not new. To overcome it, may I suggest that above sample program be added to the manual so that others in future can look up the answer there? In particular, try to stress that module returns a shape, function a value!

wolf
Two comments.

1. In OpenSCAD, modules create shapes but don't return anything.

2. Your function myVar() returns a value which is a function of choice. This is what he original poster wanted.

 Do not be mislead into thinking it reassigns a value for a variable. Calling it several times while running the script will always return the same value.
Larry