Make discovering assign() easier!

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

Make discovering assign() easier!

funnypolynomial
I just spend a frustrating couple of days discovering and then working around the whole can't-reassign-variable-values thing.  I'm an experienced programmer so when got my 3D printer a year ago I just dived into OpenSCAD and loved it.  But recently I tried to port some code (from python) and of course got cryptic syntax errors when trying to modify variables.  I understand now (kind of) and I refactored my code to be recursive and use assign() [maybe let() will make it even easier, next release?].

This clearly happens to many people (try googling!).  My question for the forum is how to make the situation better?  I suspect a minor tweak to the cheat sheet would help a lot, since it is the one thing newbies probably look at.  Consider the top-left corner
  var = value;
Firstly, "var" is a little misleading; we're not dealing with variables in the traditional programming sense.
The word "Note!" here, in red, linking to a few words about assign etc might save an enormous amount of heartache!
tp3
Reply | Threaded
Open this post in threaded view
|

Re: Make discovering assign() easier!

tp3
The assign() module will be deprecated in the next release (and already
is in the dev-snapshots).

See: http://en.wikibooks.org/wiki/OpenSCAD_User_Manual/General#Variables

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
|

Re: Make discovering assign() easier!

funnypolynomial
So to be clear, in the next release, something like this:
sum = 0;

for (i=[1:10])

{

  sum = sum + i; // <- currently a syntax error

}

echo(sum);


will work?



On Fri, Jan 9, 2015 at 11:38 AM, Torsten Paul <[hidden email]> wrote:
The assign() module will be deprecated in the next release (and already
is in the dev-snapshots).

See: http://en.wikibooks.org/wiki/OpenSCAD_User_Manual/General#Variables

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
tp3
Reply | Threaded
Open this post in threaded view
|

Re: Make discovering assign() easier!

tp3
On 01/09/2015 12:51 AM, Mark Wilson wrote:

> So to be clear, in the next release, something like this:
> sum = 0;
>
> for (i=[1:10])
> {
>    sum = sum + i; // <- currently a syntax error
> }
> echo(sum);
>
> will work?
>
Yes and no.

No: Reassignment will still behave as all the statements
are executed in parallel and one wins. OpenSCAD scripts
currently have no notion of time, so you can't really run
a sequence of statements.
(A bit similar to hardware description languages).

Yes: It's not a syntax error anymore and apart from the
reassignment it's possible to "set variables" inside the
for loop.

E.g.

a = 5;
b = 2;
for (i = [1 : 3 ]) {
     b = a + i;
     echo(a = a, b = b);
}
echo(a = a, b = b);

-----------------

ECHO: a = 5, b = 6
ECHO: a = 5, b = 7
ECHO: a = 5, b = 8
ECHO: a = 5, b = 2

Note that the statements are ordered as the evaluation is
currently still single threaded. That might change in the
future making the order dependent on the evaluation speed
in the different threads.

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
|

Re: Make discovering assign() easier!

funnypolynomial
I understand. The thing is, for many people that is a very unusual paradigm. One they are not expecting and one that the compiler doesn't warn them about (I'm assuming the next version is not going to say "Warning: you know that second 'b' is not the same as the first one, right?").

It's also something that is not very prominent in the documentation, and I'm suggesting it would help if it was.
Reply | Threaded
Open this post in threaded view
|

Re: Make discovering assign() easier!

MichaelAtOz
Administrator
Perhaps it is time to bite the bullet and refer to them as re-definable constants? They are not variables.
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: Make discovering assign() easier!

Michele
In reply to this post by funnypolynomial
The problem is that Openscad looks so much like a programming language that people (myself included) expect it to behave like a programming language.  Just like C, Basic, Lisp, Fortran, C++, Cobol, PL/I, Algol, etc., etc. support variable reassignment it's not unreasonable to assume that when you say "x = 5" that you now have a variable called x, not a constant.

If you're going to have an untyped language then there should be no difference between variables
and constants, and it seems like the assumption that 99.9 people out of 100 would make is that everything is a variable, not everything is a constant.  IMAO :-)
Reply | Threaded
Open this post in threaded view
|

Re: Make discovering assign() easier!

nophead
Think of it as a description of geometry, not a program. Nothing changes value while you are describing something.

On 9 January 2015 at 00:56, Michele <[hidden email]> wrote:
The problem is that Openscad looks so much like a programming language that
people (myself included) expect it to behave like a programming language.
Just like C, Basic, Lisp, Fortran, C++, Cobol, PL/I, Algol, etc., etc.
support variable reassignment it's not unreasonable to assume that when you
say "x = 5" that you now have a variable called x, not a constant.

If you're going to have an untyped language then there should be no
difference between variables
and constants, and it seems like the assumption that 99.9 people out of 100
would make is that everything is a variable, not everything is a constant.
IMAO :-)



--
View this message in context: http://forum.openscad.org/Make-discovering-assign-easier-tp10964p10975.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: Make discovering assign() easier!

Hypher
In reply to this post by funnypolynomial
To jump in on this discussion, I think the new functionality is pretty good and generally in-line with the functional nature of the OpenSCAD language.

I generally agree with it being slightly hard to understand, though I think the one barrier is the lack of an assignment keyword in OpenSCAD. Most folks would probably understand something like the following:

var a = 5;
var b = 2;
for (i = [1 : 3 ]) {
    var b = a + i;
    echo(a = a, b = b);
}
echo(a = a, b = b); 

But in OpenSCAD, the "var" is implicit, which can be a little confusing. Something to learn about the language.

There is, however, a more confusing variant that wouldn't be solved with a `var`-like keyword:

a = 10;
for (i = [1 : 3 ]) {
    a = a + i;
    echo(loop = 1, a = a);
}
echo(outer = 1, a = a);

outputs

ECHO: loop = 1, a = 11

ECHO: loop = 1, a = 12

ECHO: loop = 1, a = 13

ECHO: outer = 1, a = 10



This is perfectly fine. The evaluation of the expression being assigned to the inner a happens before the redefinition of a. Some languages would reject referring a variable from itself during initialization.

In any case, imho, anyone familiar with learning new languages will simply get used to the idiosyncrasies of OpenSCAD.

Coming from a functional-programming appreciator like myself, I think the behavior is great because it allows easy parallelization of the tree evaluation, while still keeping a fairly simple syntax.

- Yona

January 8, 2015 at 16:32
I understand. The thing is, for many people that is a very unusual paradigm.
One they are not expecting and one that the compiler doesn't warn them about
(I'm assuming the next version is not going to say /"Warning: you know that
second 'b' is not the same as the first one, right?"/).

It's also something that is not very prominent in the documentation, and I'm
suggesting it would help if it was.



--
View this message in context: http://forum.openscad.org/Make-discovering-assign-easier-tp10964p10973.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
January 8, 2015 at 16:07

Yes and no.

No: Reassignment will still behave as all the statements
are executed in parallel and one wins. OpenSCAD scripts
currently have no notion of time, so you can't really run
a sequence of statements.
(A bit similar to hardware description languages).

Yes: It's not a syntax error anymore and apart from the
reassignment it's possible to "set variables" inside the
for loop.

E.g.

a = 5;
b = 2;
for (i = [1 : 3 ]) {
    b = a + i;
    echo(a = a, b = b);
}
echo(a = a, b = b);

-----------------

ECHO: a = 5, b = 6
ECHO: a = 5, b = 7
ECHO: a = 5, b = 8
ECHO: a = 5, b = 2

Note that the statements are ordered as the evaluation is
currently still single threaded. That might change in the
future making the order dependent on the evaluation speed
in the different threads.

ciao,
  Torsten.


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
January 8, 2015 at 15:51
So to be clear, in the next release, something like this:
sum = 0;

for (i=[1:10])

{

  sum = sum + i; // <- currently a syntax error

}

echo(sum);


will work?




_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
January 8, 2015 at 14:38
The assign() module will be deprecated in the next release (and already
is in the dev-snapshots).

See: http://en.wikibooks.org/wiki/OpenSCAD_User_Manual/General#Variables

ciao,
  Torsten.

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
January 8, 2015 at 14:17
I just spend a frustrating couple of days discovering and then working around
the whole can't-reassign-variable-values thing. I'm an experienced
programmer so when got my 3D printer a year ago I just dived into OpenSCAD
and loved it. But recently I tried to port some code (from python) and of
course got cryptic syntax errors when trying to modify variables. I
understand now (kind of) and I refactored my code to be recursive and use
assign() [maybe let() will make it even easier, next release?].

This clearly happens to many people (try googling!). My question for the
forum is how to make the situation better? I suspect a minor tweak to the
cheat sheet would help a lot, since it is the one thing newbies probably
look at. Consider the top-left corner
var = value;
Firstly, "var" is a little misleading; we're not dealing with variables in
the traditional programming sense.
The word "Note!" here, in red, linking to a few words about assign etc might
save an enormous amount of heartache!




--
View this message in context: http://forum.openscad.org/Make-discovering-assign-easier-tp10964.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: Make discovering assign() easier!

MichaelAtOz
Administrator
In reply to this post by Michele
it is not done just to confuse people and be mean, it is integral to how the language works.

Traditional programming languages have a concept of time, loops are consecutive iteration, these terms are not in the OpenSCAD world.

Instead of iteration (say a for() loop), the individual instantiation is a tree structure, where each value of the for() 'variable' is in fact a constant for that branch of the tree.

Basically each instantiation of an object has its own scope (inherited from above in the tree) you can't pass changes up the tree to go to other branches. All the branches are evaluated ('variables' set to the last 'constant'), then the geometry for the branches are evaluated. Currently they are evaluated in order, but in the future they may be evaluated in parallel.

Basically there is no time, no now and then, there is just Is. Actually how the universe works, according to Einstein, time is an illusion.
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: Make discovering assign() easier!

MichaelAtOz
Administrator
There is also Is Not, when you get an error....
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: Make discovering assign() easier!

funnypolynomial
I'm not even suggesting the language (or the Universe) change, just that the documentation change.  A single "overview" paragraph at the start of the documentation which contained some of these concepts would Just Make Sense.