discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Special global defined in operator module can't be assigned to variable in operator scope

GC
Gareth Chen
Sun, Jul 4, 2021 8:56 AM

I've run into an issue where a special global variable defined in the
definition of an operator module (for passing to its children, for example)
cannot be used in variable assignment within the operator's scope. Here's a
lightweight example:

module wrapper(data) {
$d = data;
children();
}

module child() {
message = $d;
echo(message);
}

global = "global";
data = 1;

echo("No variable assignment");
wrapper(data) {
echo($d);
child();
}

echo("With special variable assignment");
wrapper(data) {
message = $d;
echo(message);
child();
}

echo("With global variable assignment");
wrapper(data) {
message = global;
echo(message);
child();
}

Here is the output:

ECHO: "No variable assignment"

ECHO: 1

ECHO: 1

ECHO: "With special variable assignment"

WARNING: Ignoring unknown variable '$d' in file scad, line 21

ECHO: undef

ECHO: 1

ECHO: "With global variable assignment"

ECHO: "global"

ECHO: 1

I'm trying to figure out why this happens. Right now my best guess is that
it has something to do with variables being set at compile-time or
something, but that doesn't explain why message = $d works inside of child()
.

Is this a known issue/intended behavior? Couldn't find any reference to it
in the docs.

I've run into an issue where a special global variable defined in the definition of an operator module (for passing to its children, for example) cannot be used in variable assignment within the operator's scope. Here's a lightweight example: module wrapper(data) { $d = data; children(); } module child() { message = $d; echo(message); } global = "global"; data = 1; echo("No variable assignment"); wrapper(data) { echo($d); child(); } echo("With special variable assignment"); wrapper(data) { message = $d; echo(message); child(); } echo("With global variable assignment"); wrapper(data) { message = global; echo(message); child(); } Here is the output: ECHO: "No variable assignment" ECHO: 1 ECHO: 1 ECHO: "With special variable assignment" WARNING: Ignoring unknown variable '$d' in file scad, line 21 ECHO: undef ECHO: 1 ECHO: "With global variable assignment" ECHO: "global" ECHO: 1 I'm trying to figure out why this happens. Right now my best guess is that it has something to do with variables being set at compile-time or something, but that doesn't explain why message = $d works inside of child() . Is this a known issue/intended behavior? Couldn't find any reference to it in the docs.
NH
nop head
Sun, Jul 4, 2021 12:05 PM

I think it is a long standing bug. The work around is to use let(message =
$d) { ... }

On Sun, 4 Jul 2021 at 09:57, Gareth Chen garethenator@gmail.com wrote:

I've run into an issue where a special global variable defined in the
definition of an operator module (for passing to its children, for example)
cannot be used in variable assignment within the operator's scope. Here's a
lightweight example:

module wrapper(data) {
$d = data;
children();
}

module child() {
message = $d;
echo(message);
}

global = "global";
data = 1;

echo("No variable assignment");
wrapper(data) {
echo($d);
child();
}

echo("With special variable assignment");
wrapper(data) {
message = $d;
echo(message);
child();
}

echo("With global variable assignment");
wrapper(data) {
message = global;
echo(message);
child();
}

Here is the output:

ECHO: "No variable assignment"

ECHO: 1

ECHO: 1

ECHO: "With special variable assignment"

WARNING: Ignoring unknown variable '$d' in file scad, line 21

ECHO: undef

ECHO: 1

ECHO: "With global variable assignment"

ECHO: "global"

ECHO: 1

I'm trying to figure out why this happens. Right now my best guess is that
it has something to do with variables being set at compile-time or
something, but that doesn't explain why message = $d works inside of
child().

Is this a known issue/intended behavior? Couldn't find any reference to it
in the docs.


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

I think it is a long standing bug. The work around is to use let(message = $d) { ... } On Sun, 4 Jul 2021 at 09:57, Gareth Chen <garethenator@gmail.com> wrote: > I've run into an issue where a special global variable defined in the > definition of an operator module (for passing to its children, for example) > cannot be used in variable assignment within the operator's scope. Here's a > lightweight example: > > module wrapper(data) { > $d = data; > children(); > } > > module child() { > message = $d; > echo(message); > } > > global = "global"; > data = 1; > > echo("No variable assignment"); > wrapper(data) { > echo($d); > child(); > } > > echo("With special variable assignment"); > wrapper(data) { > message = $d; > echo(message); > child(); > } > > echo("With global variable assignment"); > wrapper(data) { > message = global; > echo(message); > child(); > } > > Here is the output: > > ECHO: "No variable assignment" > > ECHO: 1 > > ECHO: 1 > > > ECHO: "With special variable assignment" > > WARNING: Ignoring unknown variable '$d' in file scad, line 21 > > ECHO: undef > > ECHO: 1 > > > ECHO: "With global variable assignment" > > ECHO: "global" > > ECHO: 1 > > > I'm trying to figure out why this happens. Right now my best guess is that > it has something to do with variables being set at compile-time or > something, but that doesn't explain why message = $d works inside of > child(). > > > Is this a known issue/intended behavior? Couldn't find any reference to it > in the docs. > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
GC
Gareth Chen
Sun, Jul 4, 2021 1:26 PM

Thanks, good to know that there's not some language feature I'm unaware of.
In the end I moved the logic into the child module and the code is cleaner
all around, but good to know that let() fixes it.

On Sun, Jul 4, 2021, 5:06 AM nop head nop.head@gmail.com wrote:

I think it is a long standing bug. The work around is to use let(message
= $d) { ... }

On Sun, 4 Jul 2021 at 09:57, Gareth Chen garethenator@gmail.com wrote:

I've run into an issue where a special global variable defined in the
definition of an operator module (for passing to its children, for example)
cannot be used in variable assignment within the operator's scope. Here's a
lightweight example:

module wrapper(data) {
$d = data;
children();
}

module child() {
message = $d;
echo(message);
}

global = "global";
data = 1;

echo("No variable assignment");
wrapper(data) {
echo($d);
child();
}

echo("With special variable assignment");
wrapper(data) {
message = $d;
echo(message);
child();
}

echo("With global variable assignment");
wrapper(data) {
message = global;
echo(message);
child();
}

Here is the output:

ECHO: "No variable assignment"

ECHO: 1

ECHO: 1

ECHO: "With special variable assignment"

WARNING: Ignoring unknown variable '$d' in file scad, line 21

ECHO: undef

ECHO: 1

ECHO: "With global variable assignment"

ECHO: "global"

ECHO: 1

I'm trying to figure out why this happens. Right now my best guess is
that it has something to do with variables being set at compile-time or
something, but that doesn't explain why message = $d works inside of
child().

Is this a known issue/intended behavior? Couldn't find any reference to
it in the docs.


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

Thanks, good to know that there's not some language feature I'm unaware of. In the end I moved the logic into the child module and the code is cleaner all around, but good to know that let() fixes it. On Sun, Jul 4, 2021, 5:06 AM nop head <nop.head@gmail.com> wrote: > I think it is a long standing bug. The work around is to use let(message > = $d) { ... } > > On Sun, 4 Jul 2021 at 09:57, Gareth Chen <garethenator@gmail.com> wrote: > >> I've run into an issue where a special global variable defined in the >> definition of an operator module (for passing to its children, for example) >> cannot be used in variable assignment within the operator's scope. Here's a >> lightweight example: >> >> module wrapper(data) { >> $d = data; >> children(); >> } >> >> module child() { >> message = $d; >> echo(message); >> } >> >> global = "global"; >> data = 1; >> >> echo("No variable assignment"); >> wrapper(data) { >> echo($d); >> child(); >> } >> >> echo("With special variable assignment"); >> wrapper(data) { >> message = $d; >> echo(message); >> child(); >> } >> >> echo("With global variable assignment"); >> wrapper(data) { >> message = global; >> echo(message); >> child(); >> } >> >> Here is the output: >> >> ECHO: "No variable assignment" >> >> ECHO: 1 >> >> ECHO: 1 >> >> >> ECHO: "With special variable assignment" >> >> WARNING: Ignoring unknown variable '$d' in file scad, line 21 >> >> ECHO: undef >> >> ECHO: 1 >> >> >> ECHO: "With global variable assignment" >> >> ECHO: "global" >> >> ECHO: 1 >> >> >> I'm trying to figure out why this happens. Right now my best guess is >> that it has something to do with variables being set at compile-time or >> something, but that doesn't explain why message = $d works inside of >> child(). >> >> >> Is this a known issue/intended behavior? Couldn't find any reference to >> it in the docs. >> _______________________________________________ >> OpenSCAD mailing list >> To unsubscribe send an email to discuss-leave@lists.openscad.org >> > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
TP
Torsten Paul
Sun, Jul 4, 2021 2:25 PM

Did you check with the snapshot version?

Did you check with the snapshot version?
GC
Gareth Chen
Sun, Jul 4, 2021 2:33 PM

No I've just been using 2021.01

On Sun, Jul 4, 2021, 7:26 AM Torsten Paul Torsten.Paul@gmx.de wrote:

Did you check with the snapshot version?


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

No I've just been using 2021.01 On Sun, Jul 4, 2021, 7:26 AM Torsten Paul <Torsten.Paul@gmx.de> wrote: > Did you check with the snapshot version? > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
NH
nop head
Sun, Jul 4, 2021 3:22 PM

See https://github.com/openscad/openscad/issues/3781#issuecomment-849064662
though. $variables defined in used files are now ignored, which make no
sense because ordinary variables in used files are in scope.

On Sun, 4 Jul 2021 at 15:34, Gareth Chen garethenator@gmail.com wrote:

No I've just been using 2021.01

On Sun, Jul 4, 2021, 7:26 AM Torsten Paul Torsten.Paul@gmx.de wrote:

Did you check with the snapshot version?


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

See https://github.com/openscad/openscad/issues/3781#issuecomment-849064662 though. $variables defined in used files are now ignored, which make no sense because ordinary variables in used files are in scope. On Sun, 4 Jul 2021 at 15:34, Gareth Chen <garethenator@gmail.com> wrote: > No I've just been using 2021.01 > > On Sun, Jul 4, 2021, 7:26 AM Torsten Paul <Torsten.Paul@gmx.de> wrote: > >> Did you check with the snapshot version? >> _______________________________________________ >> OpenSCAD mailing list >> To unsubscribe send an email to discuss-leave@lists.openscad.org >> > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
JB
Jordan Brown
Sun, Jul 4, 2021 4:27 PM

On 7/4/2021 7:25 AM, Torsten Paul wrote:

Did you check with the snapshot version?

Indeed, with the current snapshot parent-module assignments are done
before child-block assignments.  Child-block assignments are now done at
the time that the parent invokes children(), and are done once for each
invocation of children().

That solves a problem for something I was playing with last year.

On 7/4/2021 7:25 AM, Torsten Paul wrote: > Did you check with the snapshot version? Indeed, with the current snapshot parent-module assignments are done before child-block assignments.  Child-block assignments are now done at the time that the parent invokes children(), and are done once for each invocation of children(). That solves a problem for something I was playing with last year.