Variable bug?

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

Re: Variable bug?

Alan Cox
On Wed, 28 Nov 2012 17:49:41 +0100
Peter Falke <[hidden email]> wrote:

> Alan,
>
> Yup. Now how to use this for reassigning variables. I dont care if they are
> temporally expanded into a list.

That I think is to miss the fundamental model in use here. OpenSCAD
doesn't really have "variables" it merely has expansions. Its more like a
macro expansion (in fact it's a giant macro expansion into a 3D object).

That means asking the question "how do I reassign a variable" has no
meaning - its a construct of a different kind of programming.


Reply | Threaded
Open this post in threaded view
|

Re: Variable bug?

Lampbus
In reply to this post by Alan Cox
As I understand it : OpenSCAD is a language to take single input, run
through once, to build a single object = single output. To do this it
does not need 'variables'.

Programming languages are used to loop back and produce different
outputs from different inputs. Perhaps OpenSCAD could be used like that,
eg to build a whole range of model variants.

On 28/11/2012 16:09, Alan Cox wrote:

> On Wed, 28 Nov 2012 16:55:03 +0100
> Peter Falke <[hidden email]> wrote:
>
>> HI,
>>
>> thanks Marius for your reply.
>>
>> I guess the thing that is so puzzling for me is that I dont understand the
>> underlying problematic, here. With "for" the running variable can be
>> reassigned.
> It's not really reassigning the variable, its expanding the list of
> values in the "for" as a list and generating the subtree for each case.
>
> _______________________________________________
> OpenSCAD mailing list
> [hidden email]
> http://rocklinux.net/mailman/listinfo/openscad
> http://openscad.org - https://flattr.com/thing/121566
>


Reply | Threaded
Open this post in threaded view
|

Re: Variable bug?

nophead


On 28 November 2012 21:13, Richard Benjamin <[hidden email]> wrote:
As I understand it : OpenSCAD is a language to take single input, run
through once, to build a single object = single output. To do this it
does not need 'variables'.

Programming languages are used to loop back and produce different
outputs from different inputs. Perhaps OpenSCAD could be used like that,
eg to build a whole range of model variants.
 
You can do with a for loop that iterates through a list of properties. That is how I place and size the cable clips on Mendel90.

So far I haven't come across anything that needed a variable. Does anybody have an example that does? Since some functional languages are Turing complete maybe there is no problem that needs them.

On 28/11/2012 16:09, Alan Cox wrote:
> On Wed, 28 Nov 2012 16:55:03 +0100
> Peter Falke <[hidden email]> wrote:
>
>> HI,
>>
>> thanks Marius for your reply.
>>
>> I guess the thing that is so puzzling for me is that I dont understand the
>> underlying problematic, here. With "for" the running variable can be
>> reassigned.
> It's not really reassigning the variable, its expanding the list of
> values in the "for" as a list and generating the subtree for each case.
>
> _______________________________________________
> OpenSCAD mailing list
> [hidden email]
> http://rocklinux.net/mailman/listinfo/openscad
> http://openscad.org - https://flattr.com/thing/121566
>

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566

Reply | Threaded
Open this post in threaded view
|

Re: Variable bug?

Whosawhatsis
There are some things I've tried to do with complex manipulation of data that either couldn't be done or ended up being much more complicated than they should have been. Sometimes I will want to make a loop without knowing how many times it will run until it does. With a real "for" loop, I might add to/subtract from the variable being tested within the loop, or use the break/continue statements in a language that has them. I'll also often want to increment a separate variable in the loop in these cases so that I know afterwards how many times the loop ended up running. Often this math can be done ahead of time in a much more complicated way, but sometimes (with string manipulation, for example), it can't.

On Wednesday, November 28, 2012 at 3:08 PM, nop head wrote:



On 28 November 2012 21:13, Richard Benjamin <[hidden email]> wrote:
As I understand it : OpenSCAD is a language to take single input, run
through once, to build a single object = single output. To do this it
does not need 'variables'.

Programming languages are used to loop back and produce different
outputs from different inputs. Perhaps OpenSCAD could be used like that,
eg to build a whole range of model variants.
 
You can do with a for loop that iterates through a list of properties. That is how I place and size the cable clips on Mendel90.

So far I haven't come across anything that needed a variable. Does anybody have an example that does? Since some functional languages are Turing complete maybe there is no problem that needs them.

On 28/11/2012 16:09, Alan Cox wrote:
> On Wed, 28 Nov 2012 16:55:03 +0100
> Peter Falke <[hidden email]> wrote:
>
>> HI,
>>
>> thanks Marius for your reply.
>>
>> I guess the thing that is so puzzling for me is that I dont understand the
>> underlying problematic, here. With "for" the running variable can be
>> reassigned.
> It's not really reassigning the variable, its expanding the list of
> values in the "for" as a list and generating the subtree for each case.
>
> _______________________________________________
> OpenSCAD mailing list
> [hidden email]
> http://rocklinux.net/mailman/listinfo/openscad
> http://openscad.org - https://flattr.com/thing/121566
>

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


Reply | Threaded
Open this post in threaded view
|

Re: Variable bug?

Brent Burton
In reply to this post by nophead
Exactly, the error that is generated for the second assignment of C makes no sense, since B is defined right before it.

And you touch on exactly the problem: this comes up when you attempt to make a library of parts, each in separate files, and use them later but change quantities, sizes, etc defined in the part's file.

From the OpenSCAD manual:
"For instance, if you set up your shared library files to have default values defined as variables at their root level, when you include that file in your own code, you can 're-define' or override those constants by simply assigning a new value to them."

So, this is not a totally contrived example.

The difference here is that in the manual's examples the variable is always assigned a numeric expression, rather than a symbolic expression (one that refers to other vars).

I found that a good work-around, if not a better technique in general, is to properly parameterize external modules, and not try to override "internal" variables. But even that can be cumbersome, because if I have a particular material thickness required, bolt diameter, and other more global settings, I should be able to set those.



On Wed, Nov 28, 2012 at 3:07 AM, nop head <[hidden email]> wrote:
I think it is a bug because the error message makes no sense:

WARNING: Ignoring unknown variable 'B'.
ECHO: "B: ", 100
ECHO: "C Size: ", undef


This has caught me out a few times and wasted time trying to find the problem in a large multi-file project. Why does B become unknown?


Also:

C = 250; // Comment this line to hide bug
B = 100;
C = 4 + 25; // Error: unknown var 'B'
echo("B: ",B);
echo("C Size: ",C);


Gives:

ECHO: "B: ", 100
ECHO: "C Size: ", 29


I.e. no error, simply uses the second value for C throughout, and IIRC this is what the manual specifies.




On 28 November 2012 05:30, Triffid Hunter <[hidden email]> wrote:
openscad "variables" are immutable. you can only set them once. The closest we have to mutable variables are loop variables.

If you're familiar with C, think of them as defines rather than actual variables.

On Wed, Nov 28, 2012 at 2:03 PM, Brent Burton <[hidden email]> wrote:
Hi guys,
I'm new to OpenSCAD, but learning it quickly.

I stumbled upon an error in one of my scripts, and I boiled it down to this situation. The workaround is to simplify scripts to avoid this, but there may be situations, like in an if-statement, that this would occur.

/* variable bug.
* Mac OS X, OpenSCAD 2011.12
*/
C = 250;   // Comment this line to hide bug
B = 100;
C = B + 25; // Error: unknown var 'B'

echo("B: ",B);
echo("C Size: ",C);

cube([1,1,1]);


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566

Reply | Threaded
Open this post in threaded view
|

Re: Variable bug?

Peter Falke
In reply to this post by Whosawhatsis
Hi,

Here is an example of odd style code:

I was trying to make tangrams and move the pieces automatically to generate print plates.
The solution I found works without variables but It is rather odd looking.

Maybe someone has a better idea.

Basically what I did is to calculate the centre of mass for each piece and moved it outward proportionally to it from the centre of the tangram.

The problem  was to sum up all the elements of an arbitrary length vector.

(When U cange the line p=t1 to t2 or t_cross U get different tangrams.)


TakeItAndRun



On 29 November 2012 00:28, whosawhatsis <[hidden email]> wrote:
There are some things I've tried to do with complex manipulation of data that either couldn't be done or ended up being much more complicated than they should have been. Sometimes I will want to make a loop without knowing how many times it will run until it does. With a real "for" loop, I might add to/subtract from the variable being tested within the loop, or use the break/continue statements in a language that has them. I'll also often want to increment a separate variable in the loop in these cases so that I know afterwards how many times the loop ended up running. Often this math can be done ahead of time in a much more complicated way, but sometimes (with string manipulation, for example), it can't.

On Wednesday, November 28, 2012 at 3:08 PM, nop head wrote:



On 28 November 2012 21:13, Richard Benjamin <[hidden email]> wrote:
As I understand it : OpenSCAD is a language to take single input, run
through once, to build a single object = single output. To do this it
does not need 'variables'.

Programming languages are used to loop back and produce different
outputs from different inputs. Perhaps OpenSCAD could be used like that,
eg to build a whole range of model variants.
 
You can do with a for loop that iterates through a list of properties. That is how I place and size the cable clips on Mendel90.

So far I haven't come across anything that needed a variable. Does anybody have an example that does? Since some functional languages are Turing complete maybe there is no problem that needs them.

On 28/11/2012 16:09, Alan Cox wrote:
> On Wed, 28 Nov 2012 16:55:03 +0100
> Peter Falke <[hidden email]> wrote:
>
>> HI,
>>
>> thanks Marius for your reply.
>>
>> I guess the thing that is so puzzling for me is that I dont understand the
>> underlying problematic, here. With "for" the running variable can be
>> reassigned.
> It's not really reassigning the variable, its expanding the list of
> values in the "for" as a list and generating the subtree for each case.
>
> _______________________________________________
> OpenSCAD mailing list
> [hidden email]
> http://rocklinux.net/mailman/listinfo/openscad
> http://openscad.org - https://flattr.com/thing/121566
>

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566



_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566



--
[hidden email]

P.S. Falls meine E-Mail kürzer ausfällt als Dir angenehm ist:
Ich probiere gerade aus kurze Antworten statt gar keine Antworten zu schreiben.
Wenn Du gerne mehr lesen möchtest, dann lass es mich bitte wissen.

P.S. In case my e-mail is shorter than you enjoy:
I am currently trying short replies instead of no replies at all.
Please let me know, if you like to read more.

Enjoy!


Tangram.scad (3K) Download Attachment
tangram.png (9K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Variable bug?

nophead

You can sum a vector like this:


function sum(v, i = 0) = (i == len(v)) ? 0 : v[i] + sum(v, i + 1);


echo(sum([1,2,3]));

ECHO: 6


On 29 November 2012 00:24, Peter Falke <[hidden email]> wrote:
Hi,

Here is an example of odd style code:

I was trying to make tangrams and move the pieces automatically to generate print plates.
The solution I found works without variables but It is rather odd looking.

Maybe someone has a better idea.

Basically what I did is to calculate the centre of mass for each piece and moved it outward proportionally to it from the centre of the tangram.

The problem  was to sum up all the elements of an arbitrary length vector.

(When U cange the line p=t1 to t2 or t_cross U get different tangrams.)


TakeItAndRun




On 29 November 2012 00:28, whosawhatsis <[hidden email]> wrote:
There are some things I've tried to do with complex manipulation of data that either couldn't be done or ended up being much more complicated than they should have been. Sometimes I will want to make a loop without knowing how many times it will run until it does. With a real "for" loop, I might add to/subtract from the variable being tested within the loop, or use the break/continue statements in a language that has them. I'll also often want to increment a separate variable in the loop in these cases so that I know afterwards how many times the loop ended up running. Often this math can be done ahead of time in a much more complicated way, but sometimes (with string manipulation, for example), it can't.

On Wednesday, November 28, 2012 at 3:08 PM, nop head wrote:



On 28 November 2012 21:13, Richard Benjamin <[hidden email]> wrote:
As I understand it : OpenSCAD is a language to take single input, run
through once, to build a single object = single output. To do this it
does not need 'variables'.

Programming languages are used to loop back and produce different
outputs from different inputs. Perhaps OpenSCAD could be used like that,
eg to build a whole range of model variants.
 
You can do with a for loop that iterates through a list of properties. That is how I place and size the cable clips on Mendel90.

So far I haven't come across anything that needed a variable. Does anybody have an example that does? Since some functional languages are Turing complete maybe there is no problem that needs them.

On 28/11/2012 16:09, Alan Cox wrote:
> On Wed, 28 Nov 2012 16:55:03 +0100
> Peter Falke <[hidden email]> wrote:
>
>> HI,
>>
>> thanks Marius for your reply.
>>
>> I guess the thing that is so puzzling for me is that I dont understand the
>> underlying problematic, here. With "for" the running variable can be
>> reassigned.
> It's not really reassigning the variable, its expanding the list of
> values in the "for" as a list and generating the subtree for each case.
>
> _______________________________________________
> OpenSCAD mailing list
> [hidden email]
> http://rocklinux.net/mailman/listinfo/openscad
> http://openscad.org - https://flattr.com/thing/121566
>

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566



_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566



--
[hidden email]

P.S. Falls meine E-Mail kürzer ausfällt als Dir angenehm ist:
Ich probiere gerade aus kurze Antworten statt gar keine Antworten zu schreiben.
Wenn Du gerne mehr lesen möchtest, dann lass es mich bitte wissen.

P.S. In case my e-mail is shorter than you enjoy:
I am currently trying short replies instead of no replies at all.
Please let me know, if you like to read more.

Enjoy!


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566

Reply | Threaded
Open this post in threaded view
|

Re: Variable bug?

Peter Falke
Nice.

Really nice.

Thanks,

TakeItAndRun


On 29 November 2012 01:37, nop head <[hidden email]> wrote:

You can sum a vector like this:


function sum(v, i = 0) = (i == len(v)) ? 0 : v[i] + sum(v, i + 1);


echo(sum([1,2,3]));

ECHO: 6


On 29 November 2012 00:24, Peter Falke <[hidden email]> wrote:
Hi,

Here is an example of odd style code:

I was trying to make tangrams and move the pieces automatically to generate print plates.
The solution I found works without variables but It is rather odd looking.

Maybe someone has a better idea.

Basically what I did is to calculate the centre of mass for each piece and moved it outward proportionally to it from the centre of the tangram.

The problem  was to sum up all the elements of an arbitrary length vector.

(When U cange the line p=t1 to t2 or t_cross U get different tangrams.)


TakeItAndRun




On 29 November 2012 00:28, whosawhatsis <[hidden email]> wrote:
There are some things I've tried to do with complex manipulation of data that either couldn't be done or ended up being much more complicated than they should have been. Sometimes I will want to make a loop without knowing how many times it will run until it does. With a real "for" loop, I might add to/subtract from the variable being tested within the loop, or use the break/continue statements in a language that has them. I'll also often want to increment a separate variable in the loop in these cases so that I know afterwards how many times the loop ended up running. Often this math can be done ahead of time in a much more complicated way, but sometimes (with string manipulation, for example), it can't.

On Wednesday, November 28, 2012 at 3:08 PM, nop head wrote:



On 28 November 2012 21:13, Richard Benjamin <[hidden email]> wrote:
As I understand it : OpenSCAD is a language to take single input, run
through once, to build a single object = single output. To do this it
does not need 'variables'.

Programming languages are used to loop back and produce different
outputs from different inputs. Perhaps OpenSCAD could be used like that,
eg to build a whole range of model variants.
 
You can do with a for loop that iterates through a list of properties. That is how I place and size the cable clips on Mendel90.

So far I haven't come across anything that needed a variable. Does anybody have an example that does? Since some functional languages are Turing complete maybe there is no problem that needs them.

On 28/11/2012 16:09, Alan Cox wrote:
> On Wed, 28 Nov 2012 16:55:03 +0100
> Peter Falke <[hidden email]> wrote:
>
>> HI,
>>
>> thanks Marius for your reply.
>>
>> I guess the thing that is so puzzling for me is that I dont understand the
>> underlying problematic, here. With "for" the running variable can be
>> reassigned.
> It's not really reassigning the variable, its expanding the list of
> values in the "for" as a list and generating the subtree for each case.
>
> _______________________________________________
> OpenSCAD mailing list
> [hidden email]
> http://rocklinux.net/mailman/listinfo/openscad
> http://openscad.org - https://flattr.com/thing/121566
>

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566



_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566



--
[hidden email]

P.S. Falls meine E-Mail kürzer ausfällt als Dir angenehm ist:
Ich probiere gerade aus kurze Antworten statt gar keine Antworten zu schreiben.
Wenn Du gerne mehr lesen möchtest, dann lass es mich bitte wissen.

P.S. In case my e-mail is shorter than you enjoy:
I am currently trying short replies instead of no replies at all.
Please let me know, if you like to read more.

Enjoy!


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566



--
[hidden email]

P.S. Falls meine E-Mail kürzer ausfällt als Dir angenehm ist:
Ich probiere gerade aus kurze Antworten statt gar keine Antworten zu schreiben.
Wenn Du gerne mehr lesen möchtest, dann lass es mich bitte wissen.

P.S. In case my e-mail is shorter than you enjoy:
I am currently trying short replies instead of no replies at all.
Please let me know, if you like to read more.

Enjoy!

Reply | Threaded
Open this post in threaded view
|

Re: Variable bug?

Torsten Wagner
In reply to this post by Brent Burton
Hi,
Despite the question if this is a bug, feature, or annoyance, it is clearly an upcoming topic and many many people (including myself) trapped into this.
I would therefore call it a bug in terms of documentation bug. There should be more prominent description about this and maybe even be so drastic to avoid the term variables at all.
Call them openscad "defines" or openscad "units" or whatever. Most people with a programming background see in a "variable"  something which can be reassigned anytime later.

Greetings

Torwag


On 28 November 2012 12:03, Brent Burton <[hidden email]> wrote:
Hi guys,
I'm new to OpenSCAD, but learning it quickly.

I stumbled upon an error in one of my scripts, and I boiled it down to this situation. The workaround is to simplify scripts to avoid this, but there may be situations, like in an if-statement, that this would occur.

/* variable bug.
* Mac OS X, OpenSCAD 2011.12
*/
C = 250;   // Comment this line to hide bug
B = 100;
C = B + 25; // Error: unknown var 'B'

echo("B: ",B);
echo("C Size: ",C);

cube([1,1,1]);


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566

Reply | Threaded
Open this post in threaded view
|

Re: Variable bug?

Alan Cox
> Call them openscad "defines" or openscad "units" or whatever. Most people
> with a programming background see in a "variable"

Its unfortunate that people not used to functional programming are
confused by this, but it's not about a "programming background", it's
about not having a wide programming background or any maths background.

Renaming them is just going to confuse everyone instead of those without
any maths background.

Alan


Reply | Threaded
Open this post in threaded view
|

Re: Variable bug?

Alan Cox
In reply to this post by Lampbus
On Wed, 28 Nov 2012 21:13:05 +0000
Richard Benjamin <[hidden email]> wrote:

> As I understand it : OpenSCAD is a language to take single input, run
> through once, to build a single object = single output. To do this it
> does not need 'variables'.
>
> Programming languages are used to loop back and produce different
> outputs from different inputs. Perhaps OpenSCAD could be used like that,
> eg to build a whole range of model variants.

You can do that with OpenSCAD, you don't need re-assignment any more than
a mathematical equation ever decides that X should now be 5 not 4.

http://www.shapeways.com/model/736448/early-1860s-nlr-north-london-railway-coach-set.html?li=productBox-search

for example is entirely done with OpenSCAD without variables and direcly
producing the set of six vehicles in 3 different forms with various
levels of loops and parameter passing building each vehicle and building
the vehicles themselves with much of the code shared between the vehicle
types using parameter data.

There are cases its trickier but there are also huge advantages. In
particular because you don't have reassignment putting ordering
dependancies into the compilation of the object it can be parallelised
extensively.

Alan

Reply | Threaded
Open this post in threaded view
|

Re: Variable bug?

Peter Uithoven
Just to add a example of where I miss it. 
I usually have to add a offset to shapes because I want to lasercut them. So in the beginning there are basic variables like width, height, depth, material thickness and an offset. 
Then in the code I want to do
width += offset*2; 
because that makes the code more readable than using width+offset*2 everywhere. 

This is a reason why I'm keeping an eye on Rapcad, because it should be possible there. 

Best regards,
Peter Uithoven 


On Thu, Nov 29, 2012 at 3:01 PM, Alan Cox <[hidden email]> wrote:
On Wed, 28 Nov 2012 21:13:05 +0000
Richard Benjamin <[hidden email]> wrote:

> As I understand it : OpenSCAD is a language to take single input, run
> through once, to build a single object = single output. To do this it
> does not need 'variables'.
>
> Programming languages are used to loop back and produce different
> outputs from different inputs. Perhaps OpenSCAD could be used like that,
> eg to build a whole range of model variants.

You can do that with OpenSCAD, you don't need re-assignment any more than
a mathematical equation ever decides that X should now be 5 not 4.

http://www.shapeways.com/model/736448/early-1860s-nlr-north-london-railway-coach-set.html?li=productBox-search

for example is entirely done with OpenSCAD without variables and direcly
producing the set of six vehicles in 3 different forms with various
levels of loops and parameter passing building each vehicle and building
the vehicles themselves with much of the code shared between the vehicle
types using parameter data.

There are cases its trickier but there are also huge advantages. In
particular because you don't have reassignment putting ordering
dependancies into the compilation of the object it can be parallelised
extensively.

Alan
_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566

Reply | Threaded
Open this post in threaded view
|

Re: Variable bug?

Alan Cox
On Thu, 29 Nov 2012 15:09:45 +0100
Peter Uithoven <[hidden email]> wrote:

> Just to add a example of where I miss it.
> I usually have to add a offset to shapes because I want to lasercut them.
> So in the beginning there are basic variables like width, height, depth,
> material thickness and an offset.
> Then in the code I want to do
> *width += offset*2; *
> because that makes the code more readable than using *width+offset*2*
>  everywhere.

laser_width = base_width + offset * 2


A = A + B

makes no sense because its a contradiction unless B is 0

A = B + C

is just fine

(and if you can't cope with this you may want to look at the python
bindings or look at ImplicitCADs language bindings)

Alan

Reply | Threaded
Open this post in threaded view
|

Re: Variable bug?

nophead
Then in the code I want to do
width += offset*2; 
because that makes the code more readable than using width+offset*2 everywhere. 

Both are bad from a programming point of view.
Having width meaning different things in different places is bad.
Repeating the expression +offset*2 is not ideal either.

offset_width = width + 2 * offset;

Is much cleaner.

+=, in languages that support it, should be used for variables that count or iterate, where it is natural for them to have different values at different times. Not to change the meaning of a variable.

On 29 November 2012 14:24, Alan Cox <[hidden email]> wrote:
On Thu, 29 Nov 2012 15:09:45 +0100
Peter Uithoven <[hidden email]> wrote:

> Just to add a example of where I miss it.
> I usually have to add a offset to shapes because I want to l isasercut them.
> So in the beginning there are basic variables like width, height, depth,
> material thickness and an offset.
> Then in the code I want to do
> *width += offset*2; *
> because that makes the code more readable than using *width+offset*2*
>  everywhere.

laser_width = base_width + offset * 2


A = A + B

makes no sense because its a contradiction unless B is 0

A = B + C

is just fine

(and if you can't cope with this you may want to look at the python
bindings or look at ImplicitCADs language bindings)

Alan
_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566

Reply | Threaded
Open this post in threaded view
|

Re: Variable bug?

Torsten Wagner
In reply to this post by Alan Cox
Hi Alan,

just was thinking. E.g. LaTeX which is in a way rather similar in terms of programming to OpenScad, avoids the word variable 
they talk about macros or registers. 
Albeit I agree the internet is full of "How to use variables in LaTeX" questions and the good LaTeX people do not get tired to explain "LaTeX does not has variables but macros or registers...."
I guess just a big red paragraph at a prominent place in the manual would help (its there already, but hey who, with a minimum of programming skills, reads the Variable section after looking at a code example ;) )
In a perfect world, the compiler would catch such calls and return a meaningful error message ala "Variables can't be redefined (immutable)."

As for if it Openscad should get real variables. I think yes. Because, it does not harm people which prefer functional programming, they still could do (or?). Others might be just happy with real variables. 

Another idea, which came to my mind when I used OpenScad (and I guess I am not the only one), why not having a object oriented approach? 

cube1 = cube([1,1,1])
cube1.corner[:].radius = 2
cube1.surface = material.metal

cube2 = cube([2,2,2])
cube2.pos = [cube1.pos[0]+10,cube1.pos[1]-10,cube1.pos[2]-10]
cube2.corner[:].radius = 1
cube2.surface = material.wood

obj1 = cube1 -  cube2

Throw in an auto-completion and auto-doc function in the editor and it would be very easy and IMHO very logical to use. 
Kind of OT but that would maybe make it even easier for non-programming people to get used to it and it allows extension without breaking to much older syntax (just add new methods and properties over time).

Torwag


On 29 November 2012 20:56, Alan Cox <[hidden email]> wrote:
> Call them openscad "defines" or openscad "units" or whatever. Most people
> with a programming background see in a "variable"

Its unfortunate that people not used to functional programming are
confused by this, but it's not about a "programming background", it's
about not having a wide programming background or any maths background.

Renaming them is just going to confuse everyone instead of those without
any maths background.

Alan

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566

Reply | Threaded
Open this post in threaded view
|

Re: Variable bug?

tjhowse
>> the compiler would catch such calls and return a meaningful error message ala "Variables can't be redefined (immutable)."

This.


On 30 November 2012 10:53, Torsten Wagner <[hidden email]> wrote:
Hi Alan,

just was thinking. E.g. LaTeX which is in a way rather similar in terms of programming to OpenScad, avoids the word variable 
they talk about macros or registers. 
Albeit I agree the internet is full of "How to use variables in LaTeX" questions and the good LaTeX people do not get tired to explain "LaTeX does not has variables but macros or registers...."
I guess just a big red paragraph at a prominent place in the manual would help (its there already, but hey who, with a minimum of programming skills, reads the Variable section after looking at a code example ;) )
In a perfect world, the compiler would catch such calls and return a meaningful error message ala "Variables can't be redefined (immutable)."

As for if it Openscad should get real variables. I think yes. Because, it does not harm people which prefer functional programming, they still could do (or?). Others might be just happy with real variables. 

Another idea, which came to my mind when I used OpenScad (and I guess I am not the only one), why not having a object oriented approach? 

cube1 = cube([1,1,1])
cube1.corner[:].radius = 2
cube1.surface = material.metal

cube2 = cube([2,2,2])
cube2.pos = [cube1.pos[0]+10,cube1.pos[1]-10,cube1.pos[2]-10]
cube2.corner[:].radius = 1
cube2.surface = material.wood

obj1 = cube1 -  cube2

Throw in an auto-completion and auto-doc function in the editor and it would be very easy and IMHO very logical to use. 
Kind of OT but that would maybe make it even easier for non-programming people to get used to it and it allows extension without breaking to much older syntax (just add new methods and properties over time).

Torwag


On 29 November 2012 20:56, Alan Cox <[hidden email]> wrote:
> Call them openscad "defines" or openscad "units" or whatever. Most people
> with a programming background see in a "variable"

Its unfortunate that people not used to functional programming are
confused by this, but it's not about a "programming background", it's
about not having a wide programming background or any maths background.

Renaming them is just going to confuse everyone instead of those without
any maths background.

Alan

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566

Reply | Threaded
Open this post in threaded view
|

Re: Variable bug?

Whosawhatsis
Not an error, but a warning would be nice. There are several strange behaviors that don't cause an error, but could use warnings to let you know why you're not getting the output you might expect.

On Thursday, November 29, 2012 at 4:57 PM, tjhowse wrote:

>> the compiler would catch such calls and return a meaningful error message ala "Variables can't be redefined (immutable)."

This.


On 30 November 2012 10:53, Torsten Wagner <[hidden email]> wrote:
Hi Alan,

just was thinking. E.g. LaTeX which is in a way rather similar in terms of programming to OpenScad, avoids the word variable 
they talk about macros or registers. 
Albeit I agree the internet is full of "How to use variables in LaTeX" questions and the good LaTeX people do not get tired to explain "LaTeX does not has variables but macros or registers...."
I guess just a big red paragraph at a prominent place in the manual would help (its there already, but hey who, with a minimum of programming skills, reads the Variable section after looking at a code example ;) )
In a perfect world, the compiler would catch such calls and return a meaningful error message ala "Variables can't be redefined (immutable)."

As for if it Openscad should get real variables. I think yes. Because, it does not harm people which prefer functional programming, they still could do (or?). Others might be just happy with real variables. 

Another idea, which came to my mind when I used OpenScad (and I guess I am not the only one), why not having a object oriented approach? 

cube1 = cube([1,1,1])
cube1.corner[:].radius = 2
cube1.surface = material.metal

cube2 = cube([2,2,2])
cube2.pos = [cube1.pos[0]+10,cube1.pos[1]-10,cube1.pos[2]-10]
cube2.corner[:].radius = 1
cube2.surface = material.wood

obj1 = cube1 -  cube2

Throw in an auto-completion and auto-doc function in the editor and it would be very easy and IMHO very logical to use. 
Kind of OT but that would maybe make it even easier for non-programming people to get used to it and it allows extension without breaking to much older syntax (just add new methods and properties over time).

Torwag


On 29 November 2012 20:56, Alan Cox <[hidden email]> wrote:
> Call them openscad "defines" or openscad "units" or whatever. Most people
> with a programming background see in a "variable"

Its unfortunate that people not used to functional programming are
confused by this, but it's not about a "programming background", it's
about not having a wide programming background or any maths background.

Renaming them is just going to confuse everyone instead of those without
any maths background.

Alan

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


Reply | Threaded
Open this post in threaded view
|

Re: Variable bug?

Torsten Wagner
Thats true and I would even say throw an error more often.
Esp. if you have a complex object, small details can easily be overlooked. 
Suddenly, you end up missing an opening or having a not fitting part because of a little typo.
E.g. IIRC there is no error for unassigned variables. 

module oops()

{


a=1;

b=2;

thickness=1.1;

cube([a,b,thiickness]);


}

oops();


Just throws the warning WARNING: Ignoring unknown variable 'thiickness'
However, an object is still drawn taken the default value for the missing parameter, which is 1 in that case and unfortunately that close to the desired 1.1 that I doubt people see this in the final output. This can easily get ugly.

This should either throw an error omit drawing of that object.
Even better why not color objects which have problems like this in red and make them always visible? That would really indicate to people something went wrong with that object.

Torwag





On 30 November 2012 10:02, whosawhatsis <[hidden email]> wrote:
Not an error, but a warning would be nice. There are several strange behaviors that don't cause an error, but could use warnings to let you know why you're not getting the output you might expect.

On Thursday, November 29, 2012 at 4:57 PM, tjhowse wrote:

>> the compiler would catch such calls and return a meaningful error message ala "Variables can't be redefined (immutable)."

This.


On 30 November 2012 10:53, Torsten Wagner <[hidden email]> wrote:
Hi Alan,

just was thinking. E.g. LaTeX which is in a way rather similar in terms of programming to OpenScad, avoids the word variable 
they talk about macros or registers. 
Albeit I agree the internet is full of "How to use variables in LaTeX" questions and the good LaTeX people do not get tired to explain "LaTeX does not has variables but macros or registers...."
I guess just a big red paragraph at a prominent place in the manual would help (its there already, but hey who, with a minimum of programming skills, reads the Variable section after looking at a code example ;) )
In a perfect world, the compiler would catch such calls and return a meaningful error message ala "Variables can't be redefined (immutable)."

As for if it Openscad should get real variables. I think yes. Because, it does not harm people which prefer functional programming, they still could do (or?). Others might be just happy with real variables. 

Another idea, which came to my mind when I used OpenScad (and I guess I am not the only one), why not having a object oriented approach? 

cube1 = cube([1,1,1])
cube1.corner[:].radius = 2
cube1.surface = material.metal

cube2 = cube([2,2,2])
cube2.pos = [cube1.pos[0]+10,cube1.pos[1]-10,cube1.pos[2]-10]
cube2.corner[:].radius = 1
cube2.surface = material.wood

obj1 = cube1 -  cube2

Throw in an auto-completion and auto-doc function in the editor and it would be very easy and IMHO very logical to use. 
Kind of OT but that would maybe make it even easier for non-programming people to get used to it and it allows extension without breaking to much older syntax (just add new methods and properties over time).

Torwag


On 29 November 2012 20:56, Alan Cox <[hidden email]> wrote:
> Call them openscad "defines" or openscad "units" or whatever. Most people
> with a programming background see in a "variable"

Its unfortunate that people not used to functional programming are
confused by this, but it's not about a "programming background", it's
about not having a wide programming background or any maths background.

Renaming them is just going to confuse everyone instead of those without
any maths background.

Alan

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566



_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566

Reply | Threaded
Open this post in threaded view
|

Re: Variable bug?

Triffid Hunter
On Fri, Nov 30, 2012 at 12:14 PM, Torsten Wagner <[hidden email]> wrote:
Even better why not color objects which have problems like this in red and make them always visible? That would really indicate to people something went wrong with that object.

this is an excellent idea, as long as there's a corresponding warning in the log so we're not sitting there staring at the code going "umm what's wrong with it?"

Reply | Threaded
Open this post in threaded view
|

Re: Variable bug?

Torsten Wagner
Sure, warnings for everyone ;)  Just for the reason that people might call openscad from the command line without checking the graphical result.



On 30 November 2012 10:16, Triffid Hunter <[hidden email]> wrote:
On Fri, Nov 30, 2012 at 12:14 PM, Torsten Wagner <[hidden email]> wrote:
Even better why not color objects which have problems like this in red and make them always visible? That would really indicate to people something went wrong with that object.

this is an excellent idea, as long as there's a corresponding warning in the log so we're not sitting there staring at the code going "umm what's wrong with it?"


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566

1234