i copied and pasted code to reveal my experience from which I shared
interpretations within the code. the code is a "reply" located at your
original request for code.
--
View this message in context: http://forum.openscad.org/Special-Variables-tp14477p14496.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
You need to understand the concepts of lexical scope and dynamic scope. To
get something that is global you need to define it at the top level scope.
I.e. file level, outside of any functions, modules. If you assign to it
again at in the same scope it will use the last value assigned everywhere.
If you assign to it in a deeper scope it will simply define a new constant
that hides the original.
A normal variable defined at file scope is visible everywhere later in that
file, but not in modules and functions in other files, called from that
file. They have lexical scope meaning you can see where they are visible
simply by looking at the static program structure. Each module, function
and block enclosed by braces is a new scope. Inside that scope you can
access names defined in scopes that enclose it.
The only difference with $ prefixed names is they have dynamic scope
meaning they are visible not only in the lexical scope they are defined in
but also in any modules or functions called from that scope, even if they
are in different files. But again if you define a new one in a deeper scope
it hides the original. It doesn't reassign the original. And constants
defined in a deeper scope disappear when that scope ends. They never leak
from an inner scope to an outer one.
On Nov 15, 2015 1:54 AM, "whburling" whburling@outlook.com wrote:
i copied and pasted code to reveal my experience from which I shared
interpretations within the code. the code is a "reply" located at your
original request for code.
--
View this message in context:
http://forum.openscad.org/Special-Variables-tp14477p14496.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Perhaps this example will help
External file Global Test.scad
module Test1(){
cube(4);
echo(" in Test1 x1 = ",x1);
echo(" in Test1 $y1 = ",$y1);
}
module Test2(){
sphere(3);
echo(" in Test2 x2 = ",x2);
echo(" in Test2 $y2 = ",$y2);
}
x1 = 3;
$y1 = 5;
Local file
use<Global Test.scad>;
Test1();
Test2();
Test3();
x1 = 10;
$y1 = 15;
x2 = 20;
$y2 = 27;
x3 = 30;
$y3 = 33;
module Test3(){
cylinder(10);
echo(" in Test3 x3 = ",x3);
echo(" in Test3 $y3 = ",$y3);
}
Result
ECHO: " in Test1 x1 = ", 3 // Values from Global Test.scad, no
override
ECHO: " in Test1 $y1 = ", 5
ECHO: " in Test2 x2 = ", undef
ECHO: " in Test2 $y2 = ", 27 // local value used in external module
ECHO: " in Test3 x3 = ", 30 // local values used in local moduel
ECHO: " in Test3 $y3 = ", 33
--
View this message in context: http://forum.openscad.org/Special-Variables-tp14477p14512.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
if you replace use<Global Test.scad>; with include<Global Test.scad>; the
results are slightly different.
ECHO: " in Test1 x1 = ", 10
ECHO: " in Test1 $y1 = ", 15
ECHO: " in Test2 x2 = ", 20
ECHO: " in Test2 $y2 = ", 27
ECHO: " in Test3 x3 = ", 30
ECHO: " in Test3 $y3 = ", 33
Everyone uses the local values, since the included file contents are now
treated as part of the local file.
--
View this message in context: http://forum.openscad.org/Special-Variables-tp14477p14517.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
The real problem with 'use' is that it doesn't import variable definitions,
only function and module definitions.
On 15 November 2015 at 13:26, L Boyd lboyd@frontiernet.net wrote:
if you replace use<Global Test.scad>; with include<Global Test.scad>; the
results are slightly different.
ECHO: " in Test1 x1 = ", 10
ECHO: " in Test1 $y1 = ", 15
ECHO: " in Test2 x2 = ", 20
ECHO: " in Test2 $y2 = ", 27
ECHO: " in Test3 x3 = ", 30
ECHO: " in Test3 $y3 = ", 33
Everyone uses the local values, since the included file contents are now
treated as part of the local file.
--
View this message in context:
http://forum.openscad.org/Special-Variables-tp14477p14517.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
That is a pro, not a con. Better encapsulation.
On Nov 15, 2015 7:25 PM, "doug moen" doug@moens.org wrote:
The real problem with 'use' is that it doesn't import variable
definitions, only function and module definitions.
On 15 November 2015 at 13:26, L Boyd lboyd@frontiernet.net wrote:
if you replace use<Global Test.scad>; with include<Global Test.scad>; the
results are slightly different.
ECHO: " in Test1 x1 = ", 10
ECHO: " in Test1 $y1 = ", 15
ECHO: " in Test2 x2 = ", 20
ECHO: " in Test2 $y2 = ", 27
ECHO: " in Test3 x3 = ", 30
ECHO: " in Test3 $y3 = ", 33
Everyone uses the local values, since the included file contents are now
treated as part of the local file.
--
View this message in context:
http://forum.openscad.org/Special-Variables-tp14477p14517.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
It's definitely a feature. I'm in the habit of coding my utility and library scad files to "self test" when rendered directly or via include, and "use" them across project and variant boundaries. If they depend on top-level variable values then they need rework to be more portable.
Anything that needs to be treated like a global reference or design value shareable across multiple projects, I code as a 0-argument function.
function pi() =3.141592;
It also allowed you do things like incorporate tolerances, design-for-manufacturing rules (min spacing, wall thickness, etc) as arguments to the function-definitions.
Andrew
Sent from myBrain
On Nov 15, 2015, at 17:38, nop head nop.head@gmail.com wrote:
That is a pro, not a con. Better encapsulation.
On Nov 15, 2015 7:25 PM, "doug moen" doug@moens.org wrote:
The real problem with 'use' is that it doesn't import variable definitions, only function and module definitions.
On 15 November 2015 at 13:26, L Boyd lboyd@frontiernet.net wrote:
if you replace use<Global Test.scad>; with include<Global Test.scad>; the
results are slightly different.
ECHO: " in Test1 x1 = ", 10
ECHO: " in Test1 $y1 = ", 15
ECHO: " in Test2 x2 = ", 20
ECHO: " in Test2 $y2 = ", 27
ECHO: " in Test3 x3 = ", 30
ECHO: " in Test3 $y3 = ", 33
Everyone uses the local values, since the included file contents are now
treated as part of the local file.
--
View this message in context: http://forum.openscad.org/Special-Variables-tp14477p14517.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
I have difficulty understanding how the current design of 'use' is a "pro",
and I'm skeptical about the claim that it supports "better encapsulation".
If we really want to support better encapsulation in libraries, then we
should provide a way for libraries to control which functions, modules and
variables are exported, and which are private. I'd be happy with something
as simple as a naming convention: names beginning with _ or $ are private.
My current hatred of 'use' comes from my struggles to figure out how to use
the MCAD library. This is the standard library that ships with OpenSCAD.
From the README for MCAD, which is part of the OpenSCAD distro:
You can import these files in your scripts with `use
<MCAD/filename.scad>`,
where 'filename' is one of the files listed below like 'motors' or
'servos'.
This implies that the purpose of 'use' is for importing a library file, but
it's all a big lie.
In order to successfully use an MCAD library file, you must first read the
file, reverse engineer it, and determine whether the author intended it to
be referenced via 'use' or 'include'. Some library files work only with
'use', some work only with 'include'.
https://github.com/doug-moen/openscad2/blob/master/rfc/Library_Scripts.md#referencing-a-model-script
On 15 November 2015 at 17:38, nop head nop.head@gmail.com wrote:
That is a pro, not a con. Better encapsulation.
On Nov 15, 2015 7:25 PM, "doug moen" doug@moens.org wrote:
The real problem with 'use' is that it doesn't import variable
definitions, only function and module definitions.
On 15 November 2015 at 13:26, L Boyd lboyd@frontiernet.net wrote:
if you replace use<Global Test.scad>; with include<Global Test.scad>; the
results are slightly different.
ECHO: " in Test1 x1 = ", 10
ECHO: " in Test1 $y1 = ", 15
ECHO: " in Test2 x2 = ", 20
ECHO: " in Test2 $y2 = ", 27
ECHO: " in Test3 x3 = ", 30
ECHO: " in Test3 $y3 = ", 33
Everyone uses the local values, since the included file contents are now
treated as part of the local file.
--
View this message in context:
http://forum.openscad.org/Special-Variables-tp14477p14517.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
I suspect library modules should be implemented as two files, one that you
include to get constants and one that you use to get functions and modules.
Note that even if you use a module you can still override constants in it
by passing them as extra parameters.
On Nov 15, 2015 11:52 PM, "doug moen" doug@moens.org wrote:
I have difficulty understanding how the current design of 'use' is a
"pro", and I'm skeptical about the claim that it supports "better
encapsulation".
If we really want to support better encapsulation in libraries, then we
should provide a way for libraries to control which functions, modules and
variables are exported, and which are private. I'd be happy with something
as simple as a naming convention: names beginning with _ or $ are private.
My current hatred of 'use' comes from my struggles to figure out how to
use the MCAD library. This is the standard library that ships with
OpenSCAD. From the README for MCAD, which is part of the OpenSCAD distro:
You can import these files in your scripts with `use
<MCAD/filename.scad>`,
where 'filename' is one of the files listed below like 'motors' or
'servos'.
This implies that the purpose of 'use' is for importing a library file,
but it's all a big lie.
In order to successfully use an MCAD library file, you must first read the
file, reverse engineer it, and determine whether the author intended it to
be referenced via 'use' or 'include'. Some library files work only with
'use', some work only with 'include'.
- Some MCAD scripts export mathematical constants as part of their
API. You must use include. Examples are math.scad and materials.scad.
- Some MCAD scripts define mathematical constants that are used
locally within their functions and modules. They won't work with use,
you must use include. Examples are lego_compatibiity.scad,
involute_gears.scad.
- Some MCAD scripts have top level geometry as example code. They are
designed to work with use. For example, trochoids.scad contains demo
code with top level geometry and parameter definitions, including a
definition of $fn. These parameter definitions are intended to be
invisible when the script is used.
https://github.com/doug-moen/openscad2/blob/master/rfc/Library_Scripts.md#referencing-a-model-script
On 15 November 2015 at 17:38, nop head nop.head@gmail.com wrote:
That is a pro, not a con. Better encapsulation.
On Nov 15, 2015 7:25 PM, "doug moen" doug@moens.org wrote:
The real problem with 'use' is that it doesn't import variable
definitions, only function and module definitions.
On 15 November 2015 at 13:26, L Boyd lboyd@frontiernet.net wrote:
if you replace use<Global Test.scad>; with include<Global Test.scad>;
the
results are slightly different.
ECHO: " in Test1 x1 = ", 10
ECHO: " in Test1 $y1 = ", 15
ECHO: " in Test2 x2 = ", 20
ECHO: " in Test2 $y2 = ", 27
ECHO: " in Test3 x3 = ", 30
ECHO: " in Test3 $y3 = ", 33
Everyone uses the local values, since the included file contents are now
treated as part of the local file.
--
View this message in context:
http://forum.openscad.org/Special-Variables-tp14477p14517.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
nophead wrote
Note that even if you use a module you can still override constants in it
by passing them as extra parameters.
Decided to test this using my example posted earlier.
part of Global Test.scad
module Test1(){
cube(4);
echo(" in Test1 x1 = ",x1);
echo(" in Test1 $y1 = ",$y1);
}
x1 = 3;
$y1 = 5;
Local file
use<Global Test.scad>;
Test1();
ECHO: " in Test1 x1 = ", 3
ECHO: " in Test1 $y1 = ", 5
Test1(x1=19);
ECHO: " in Test1 x1 = ", 19
ECHO: " in Test1 $y1 = ", 5
Test1(x1=192,$y1=157);
ECHO: " in Test1 x1 = ", 192
ECHO: " in Test1 $y1 = ", 157
Test1(); // after all of the above
ECHO: " in Test1 x1 = ", 3
ECHO: " in Test1 $y1 = ", 5
I was able to temporarily override the internal values of x1 and $y1 even
thought they did not appear as parameters in the definition of Test1()!
--
View this message in context: http://forum.openscad.org/Special-Variables-tp14477p14538.html
Sent from the OpenSCAD mailing list archive at Nabble.com.