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 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
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
Did you check with the snapshot version?
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
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
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.