discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Scope rules broken

NH
nop head
Sun, Jan 10, 2016 1:57 PM

To get rid of warnings I changed my assigns to assignment inside braces but
it doesn't work if the braces are otherwise redundant. I.e. braces don't
create a new scope, only control statements like if and for. Surely this is
wrong?

module test() {
assign(x = 0) echo(x);
assign(x = 1) echo(x);
}

test();

module test2() {
{ x = 0; echo(x); }
{ x = 1; echo(x); }
}

test2();

module test3() {
if(1){ x = 0; echo(x); }
if(1){ x = 1; echo(x); }
}

test3();

DEPRECATED: The assign() module will be removed in future releases. Use a
regular assignment instead.

ECHO: 0

ECHO: 1

ECHO: 1 <==== should be 0

ECHO: 1

ECHO: 0

ECHO: 1

To get rid of warnings I changed my assigns to assignment inside braces but it doesn't work if the braces are otherwise redundant. I.e. braces don't create a new scope, only control statements like if and for. Surely this is wrong? module test() { assign(x = 0) echo(x); assign(x = 1) echo(x); } test(); module test2() { { x = 0; echo(x); } { x = 1; echo(x); } } test2(); module test3() { if(1){ x = 0; echo(x); } if(1){ x = 1; echo(x); } } test3(); DEPRECATED: The assign() module will be removed in future releases. Use a regular assignment instead. ECHO: 0 ECHO: 1 ECHO: 1 <==== should be 0 ECHO: 1 ECHO: 0 ECHO: 1
NH
nop head
Sun, Jan 10, 2016 2:19 PM

My workaround is to use for in place of assign. It seems to be an exact
replacement when the values is a scalar.

On 10 January 2016 at 13:57, nop head nop.head@gmail.com wrote:

To get rid of warnings I changed my assigns to assignment inside braces
but it doesn't work if the braces are otherwise redundant. I.e. braces
don't create a new scope, only control statements like if and for. Surely
this is wrong?

module test() {
assign(x = 0) echo(x);
assign(x = 1) echo(x);
}

test();

module test2() {
{ x = 0; echo(x); }
{ x = 1; echo(x); }
}

test2();

module test3() {
if(1){ x = 0; echo(x); }
if(1){ x = 1; echo(x); }
}

test3();

DEPRECATED: The assign() module will be removed in future releases. Use a
regular assignment instead.

ECHO: 0

ECHO: 1

ECHO: 1 <==== should be 0

ECHO: 1

ECHO: 0

ECHO: 1

My workaround is to use for in place of assign. It seems to be an exact replacement when the values is a scalar. On 10 January 2016 at 13:57, nop head <nop.head@gmail.com> wrote: > To get rid of warnings I changed my assigns to assignment inside braces > but it doesn't work if the braces are otherwise redundant. I.e. braces > don't create a new scope, only control statements like if and for. Surely > this is wrong? > > module test() { > assign(x = 0) echo(x); > assign(x = 1) echo(x); > } > > test(); > > module test2() { > { x = 0; echo(x); } > { x = 1; echo(x); } > } > > test2(); > > module test3() { > if(1){ x = 0; echo(x); } > if(1){ x = 1; echo(x); } > } > > test3(); > > DEPRECATED: The assign() module will be removed in future releases. Use a > regular assignment instead. > > ECHO: 0 > > ECHO: 1 > > ECHO: 1 <==== should be 0 > > ECHO: 1 > > ECHO: 0 > > ECHO: 1 > > > >
TP
Torsten Paul
Sun, Jan 10, 2016 3:28 PM

On 01/10/2016 02:57 PM, nop head wrote:

To get rid of warnings I changed my assigns to assignment
inside braces but it doesn't work if the braces are otherwise
redundant. I.e. braces don't create a new scope, only control
statements like if and for. Surely this is wrong?

I think the key is "backward compatibility". It was used in
scripts before to group variables. Introducing a new scope
now, would break all those scripts.

I guess otherwise it would indeed be the preferred way to
create a new scope.

This is mentioned in the manual as "anonymous scopes"
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/General#Scope_of_variables

ciao,
Torsten.

On 01/10/2016 02:57 PM, nop head wrote: > To get rid of warnings I changed my assigns to assignment > inside braces but it doesn't work if the braces are otherwise > redundant. I.e. braces don't create a new scope, only control > statements like if and for. Surely this is wrong? > I think the key is "backward compatibility". It was used in scripts before to group variables. Introducing a new scope now, would break all those scripts. I guess otherwise it would indeed be the preferred way to create a new scope. This is mentioned in the manual as "anonymous scopes" https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/General#Scope_of_variables ciao, Torsten.
DM
doug moen
Sun, Jan 10, 2016 4:20 PM

I discovered this earlier in the year. I agree it's a bug. OpenSCAD copies
it's syntax from the C-like family of languages, and in those languages,
{..} introduces a new scope. But some people discovered the bug, started
treating it as a feature (for grouping variables). And now the wiki
documents this as a feature.

Nop Head mentions that for(x=1,y=2) {...} is a substitute for introducing a
scope. I think this is another bug: we should report an error if you try to
iterate over something that is not a list or range.

So are we now going to update the wiki, recommending that people replace
assign(x=1,y=2){...} with for(x=1,y=2){...}? Yuck.

If we are going down this path, then I request that we add a let statement,
consistent with the let expression, so that you can use let(x=1,y=2){...}
instead of abusing the for statement.

On 10 January 2016 at 10:28, Torsten Paul Torsten.Paul@gmx.de wrote:

On 01/10/2016 02:57 PM, nop head wrote:

To get rid of warnings I changed my assigns to assignment
inside braces but it doesn't work if the braces are otherwise
redundant. I.e. braces don't create a new scope, only control
statements like if and for. Surely this is wrong?

I think the key is "backward compatibility". It was used in
scripts before to group variables. Introducing a new scope
now, would break all those scripts.

I guess otherwise it would indeed be the preferred way to
create a new scope.

This is mentioned in the manual as "anonymous scopes"

https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/General#Scope_of_variables

ciao,
Torsten.


OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

I discovered this earlier in the year. I agree it's a bug. OpenSCAD copies it's syntax from the C-like family of languages, and in those languages, {..} introduces a new scope. But some people discovered the bug, started treating it as a feature (for grouping variables). And now the wiki documents this as a feature. Nop Head mentions that for(x=1,y=2) {...} is a substitute for introducing a scope. I think this is another bug: we should report an error if you try to iterate over something that is not a list or range. So are we now going to update the wiki, recommending that people replace assign(x=1,y=2){...} with for(x=1,y=2){...}? Yuck. If we are going down this path, then I request that we add a let statement, consistent with the let expression, so that you can use let(x=1,y=2){...} instead of abusing the for statement. On 10 January 2016 at 10:28, Torsten Paul <Torsten.Paul@gmx.de> wrote: > On 01/10/2016 02:57 PM, nop head wrote: > > To get rid of warnings I changed my assigns to assignment > > inside braces but it doesn't work if the braces are otherwise > > redundant. I.e. braces don't create a new scope, only control > > statements like if and for. Surely this is wrong? > > > I think the key is "backward compatibility". It was used in > scripts before to group variables. Introducing a new scope > now, would break all those scripts. > > I guess otherwise it would indeed be the preferred way to > create a new scope. > > This is mentioned in the manual as "anonymous scopes" > > https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/General#Scope_of_variables > > ciao, > Torsten. > > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org > > >
NH
nop head
Sun, Jan 10, 2016 4:24 PM

Perhaps assign should not have been deprecated then as I now have to
replace them with for and there is no way of assigning a list.

On 10 January 2016 at 15:28, Torsten Paul Torsten.Paul@gmx.de wrote:

On 01/10/2016 02:57 PM, nop head wrote:

To get rid of warnings I changed my assigns to assignment
inside braces but it doesn't work if the braces are otherwise
redundant. I.e. braces don't create a new scope, only control
statements like if and for. Surely this is wrong?

I think the key is "backward compatibility". It was used in
scripts before to group variables. Introducing a new scope
now, would break all those scripts.

I guess otherwise it would indeed be the preferred way to
create a new scope.

This is mentioned in the manual as "anonymous scopes"

https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/General#Scope_of_variables

ciao,
Torsten.


OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

Perhaps assign should not have been deprecated then as I now have to replace them with for and there is no way of assigning a list. On 10 January 2016 at 15:28, Torsten Paul <Torsten.Paul@gmx.de> wrote: > On 01/10/2016 02:57 PM, nop head wrote: > > To get rid of warnings I changed my assigns to assignment > > inside braces but it doesn't work if the braces are otherwise > > redundant. I.e. braces don't create a new scope, only control > > statements like if and for. Surely this is wrong? > > > I think the key is "backward compatibility". It was used in > scripts before to group variables. Introducing a new scope > now, would break all those scripts. > > I guess otherwise it would indeed be the preferred way to > create a new scope. > > This is mentioned in the manual as "anonymous scopes" > > https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/General#Scope_of_variables > > ciao, > Torsten. > > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org >
TP
Torsten Paul
Sun, Jan 10, 2016 4:35 PM

On 01/10/2016 05:20 PM, doug moen wrote:

If we are going down this path, then I request that we
add a let statement, consistent with the let expression,
so that you can use let(x=1,y=2){...} instead of abusing
the for statement.

Good point. With having let() executing the assignments
sequentially, that would also prevent that nesting which
is needed for assign() if the variables are dependent.

This is included in the OpenSCAD2 proposal already, right?
It should be trivial to add to the existing version too.

ciao,
Torsten.

On 01/10/2016 05:20 PM, doug moen wrote: > If we are going down this path, then I request that we > add a let statement, consistent with the let expression, > so that you can use let(x=1,y=2){...} instead of abusing > the for statement. > Good point. With having let() executing the assignments sequentially, that would also prevent that nesting which is needed for assign() if the variables are dependent. This is included in the OpenSCAD2 proposal already, right? It should be trivial to add to the existing version too. ciao, Torsten.
DM
doug moen
Sun, Jan 10, 2016 4:35 PM

The difference between 'assign' and 'let' is that 'let' supports
"sequential scoping", so you can write let(x=1,y=x*2).

I know that Marius is keen to support sequential scoping everywhere that it
makes sense, and I think that may be a motivation for deprecating assign.

On 10 January 2016 at 11:24, nop head nop.head@gmail.com wrote:

Perhaps assign should not have been deprecated then as I now have to
replace them with for and there is no way of assigning a list.

On 10 January 2016 at 15:28, Torsten Paul Torsten.Paul@gmx.de wrote:

On 01/10/2016 02:57 PM, nop head wrote:

To get rid of warnings I changed my assigns to assignment
inside braces but it doesn't work if the braces are otherwise
redundant. I.e. braces don't create a new scope, only control
statements like if and for. Surely this is wrong?

I think the key is "backward compatibility". It was used in
scripts before to group variables. Introducing a new scope
now, would break all those scripts.

I guess otherwise it would indeed be the preferred way to
create a new scope.

This is mentioned in the manual as "anonymous scopes"

https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/General#Scope_of_variables

ciao,
Torsten.


OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

The difference between 'assign' and 'let' is that 'let' supports "sequential scoping", so you can write let(x=1,y=x*2). I know that Marius is keen to support sequential scoping everywhere that it makes sense, and I think that may be a motivation for deprecating assign. On 10 January 2016 at 11:24, nop head <nop.head@gmail.com> wrote: > Perhaps assign should not have been deprecated then as I now have to > replace them with for and there is no way of assigning a list. > > On 10 January 2016 at 15:28, Torsten Paul <Torsten.Paul@gmx.de> wrote: > >> On 01/10/2016 02:57 PM, nop head wrote: >> > To get rid of warnings I changed my assigns to assignment >> > inside braces but it doesn't work if the braces are otherwise >> > redundant. I.e. braces don't create a new scope, only control >> > statements like if and for. Surely this is wrong? >> > >> I think the key is "backward compatibility". It was used in >> scripts before to group variables. Introducing a new scope >> now, would break all those scripts. >> >> I guess otherwise it would indeed be the preferred way to >> create a new scope. >> >> This is mentioned in the manual as "anonymous scopes" >> >> https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/General#Scope_of_variables >> >> ciao, >> Torsten. >> >> >> _______________________________________________ >> OpenSCAD mailing list >> Discuss@lists.openscad.org >> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org >> > > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org > >
NH
nop head
Sun, Jan 10, 2016 4:58 PM

A bit premature then to deprecate it before adding let.

On 10 January 2016 at 16:35, doug moen doug@moens.org wrote:

The difference between 'assign' and 'let' is that 'let' supports
"sequential scoping", so you can write let(x=1,y=x*2).

I know that Marius is keen to support sequential scoping everywhere that
it makes sense, and I think that may be a motivation for deprecating assign.

On 10 January 2016 at 11:24, nop head nop.head@gmail.com wrote:

Perhaps assign should not have been deprecated then as I now have to
replace them with for and there is no way of assigning a list.

On 10 January 2016 at 15:28, Torsten Paul Torsten.Paul@gmx.de wrote:

On 01/10/2016 02:57 PM, nop head wrote:

To get rid of warnings I changed my assigns to assignment
inside braces but it doesn't work if the braces are otherwise
redundant. I.e. braces don't create a new scope, only control
statements like if and for. Surely this is wrong?

I think the key is "backward compatibility". It was used in
scripts before to group variables. Introducing a new scope
now, would break all those scripts.

I guess otherwise it would indeed be the preferred way to
create a new scope.

This is mentioned in the manual as "anonymous scopes"

https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/General#Scope_of_variables

ciao,
Torsten.


OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

A bit premature then to deprecate it before adding let. On 10 January 2016 at 16:35, doug moen <doug@moens.org> wrote: > The difference between 'assign' and 'let' is that 'let' supports > "sequential scoping", so you can write let(x=1,y=x*2). > > I know that Marius is keen to support sequential scoping everywhere that > it makes sense, and I think that may be a motivation for deprecating assign. > > On 10 January 2016 at 11:24, nop head <nop.head@gmail.com> wrote: > >> Perhaps assign should not have been deprecated then as I now have to >> replace them with for and there is no way of assigning a list. >> >> On 10 January 2016 at 15:28, Torsten Paul <Torsten.Paul@gmx.de> wrote: >> >>> On 01/10/2016 02:57 PM, nop head wrote: >>> > To get rid of warnings I changed my assigns to assignment >>> > inside braces but it doesn't work if the braces are otherwise >>> > redundant. I.e. braces don't create a new scope, only control >>> > statements like if and for. Surely this is wrong? >>> > >>> I think the key is "backward compatibility". It was used in >>> scripts before to group variables. Introducing a new scope >>> now, would break all those scripts. >>> >>> I guess otherwise it would indeed be the preferred way to >>> create a new scope. >>> >>> This is mentioned in the manual as "anonymous scopes" >>> >>> https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/General#Scope_of_variables >>> >>> ciao, >>> Torsten. >>> >>> >>> _______________________________________________ >>> OpenSCAD mailing list >>> Discuss@lists.openscad.org >>> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org >>> >> >> >> _______________________________________________ >> OpenSCAD mailing list >> Discuss@lists.openscad.org >> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org >> >> > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org > >
TP
Torsten Paul
Sun, Jan 10, 2016 7:33 PM

On 01/10/2016 05:20 PM, doug moen wrote:

If we are going down this path, then I request that we add a
let statement, consistent with the let expression, so that you
can use let(x=1,y=2){...} instead of abusing the for statement.

On 01/10/2016 05:20 PM, doug moen wrote: > If we are going down this path, then I request that we add a > let statement, consistent with the let expression, so that you > can use let(x=1,y=2){...} instead of abusing the for statement. > There it is https://github.com/openscad/openscad/pull/1549 ciao, Torsten.
MK
Marius Kintel
Sun, Jan 10, 2016 8:07 PM

To confirm/clarify:

The anonymous scopes was a design mistake in early OpenSCAD, where scopes didn’t have a function.
I don’t know how often people use that feature to group variables, so I haven’t dared changing that yet, but I do agree that ideally this should be a true scope.

Deprecating assert() was indeed a bit premature, but it functions well as a way of telling people to avoid using it.
The parallel assignment behavior was an extra motivator to deprecate it. Adding let() makes sense to me and that would also be another baby step towards OpenSCAD2 :)

-Marius

To confirm/clarify: The anonymous scopes was a design mistake in early OpenSCAD, where scopes didn’t have a function. I don’t know how often people use that feature to group variables, so I haven’t dared changing that yet, but I do agree that ideally this should be a true scope. Deprecating assert() was indeed a bit premature, but it functions well as a way of telling people to avoid using it. The parallel assignment behavior was an extra motivator to deprecate it. Adding let() makes sense to me and that would also be another baby step towards OpenSCAD2 :) -Marius