discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Special Variables

NH
nop head
Wed, Nov 18, 2015 9:43 PM

I think all three are true but I don't have access to a computer at the
moment to verify that.

If you have use for a function or module that is only needed inside another
module it is better to define it there so it doesn't pollute the global
namespace. If 1 is true as I suspect then that would also be a reason for
nesting.
On Nov 18, 2015 9:01 PM, "L Boyd" lboyd@frontiernet.net wrote:

nophead wrote

Note that functions and modules don't have to defined at file scope. You
can define them in modules as well.

Learn something new every day!

Have not tried yet, but I assume this means the universe for these
functions
and modules are the scope within which they were defined. This would mean:

  1. Regular variables have the value in effect at this scope, which may be
    different than the value at file scope.
  2. Functions and modules thus defined are undefined outside of this
    universe.
  3. The value of a special variable is still dependent on the call
    hierarchy,
    not where the function or module was defined.

Not sure how to use this knowledge, but  it means some rewording where I
said "outer most scope".

Do you have some ideas where they might be useful?

L Boyd wrote

The following is based on testing using OpenSCAD version 2015.03-1.

Inside functions and modules, special variables behave differently than
normal variables.

User-Defined Functions and Modules are defined at the outer most scope

of

the script.
They see and use the values of regular variables in effect at this outer
scope. New values assigned within inner scopes, from which they are
called,
are not seen by the functions and modules.

When external files are loaded with include they are treated as if they
were
internal, using the same values of regular variables as if locally
defined.

When run is used to load only the functions and modules from an external
file, they use the values of regular variables from their external file,
rather than the local values.

Variables inside a single instance of a function or module can be
overridden
by passing as parameters in the call.

For example
var = 5;
module showvar() echo(var);
showvar();          // ECHO: 5
showvar(var = 10);  // ECHO: 10

showvar(var = 10); is equivalent to:
module showvar(){ var = 10; echo(var); }
showvar();

With special variables, the functions and modules DO see new values
assigned within the inner scope from which they were called. This
includes
functions and modules loaded with run as well as those included and
locally
defined.


Larry

View this message in context:
http://forum.openscad.org/Special-Variables-tp14477p14628.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 think all three are true but I don't have access to a computer at the moment to verify that. If you have use for a function or module that is only needed inside another module it is better to define it there so it doesn't pollute the global namespace. If 1 is true as I suspect then that would also be a reason for nesting. On Nov 18, 2015 9:01 PM, "L Boyd" <lboyd@frontiernet.net> wrote: > nophead wrote > > Note that functions and modules don't have to defined at file scope. You > > can define them in modules as well. > > Learn something new every day! > > Have not tried yet, but I assume this means the universe for these > functions > and modules are the scope within which they were defined. This would mean: > 1. Regular variables have the value in effect at this scope, which may be > different than the value at file scope. > 2. Functions and modules thus defined are undefined outside of this > universe. > 3. The value of a special variable is still dependent on the call > hierarchy, > not where the function or module was defined. > > Not sure how to use this knowledge, but it means some rewording where I > said "outer most scope". > > Do you have some ideas where they might be useful? > > > > L Boyd wrote > >> The following is based on testing using OpenSCAD version 2015.03-1. > >> > >> Inside functions and modules, special variables behave differently than > >> normal variables. > >> > >> User-Defined Functions and Modules are defined at the outer most scope > of > >> the script. > >> They see and use the values of regular variables in effect at this outer > >> scope. New values assigned within inner scopes, from which they are > >> called, > >> are *not seen* by the functions and modules. > >> > >> When external files are loaded with include they are treated as if they > >> were > >> internal, using the same values of regular variables as if locally > >> defined. > >> > >> When run is used to load only the functions and modules from an external > >> file, they use the values of regular variables from their external file, > >> rather than the local values. > >> > >> Variables inside a single instance of a function or module can be > >> overridden > >> by passing as parameters in the call. > >> > >> For example > >> var = 5; > >> module showvar() echo(var); > >> showvar(); // ECHO: 5 > >> showvar(var = 10); // ECHO: 10 > >> > >> showvar(var = 10); is equivalent to: > >> module showvar(){ var = 10; echo(var); } > >> showvar(); > >> > >> With special variables, the functions and modules *DO* see new values > >> assigned within the inner scope from which they were called. This > >> includes > >> functions and modules loaded with run as well as those included and > >> locally > >> defined. > > > > > > ----- > Larry > -- > View this message in context: > http://forum.openscad.org/Special-Variables-tp14477p14628.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 >
M
MichaelAtOz
Thu, Nov 19, 2015 8:57 PM

L Boyd wrote

For example
var = 5;
module showvar() echo(var);
showvar();         // ECHO: 5
showvar(var = 10); // ECHO: 10

showvar(var = 10); is equivalent to:
module showvar(){ var = 10; echo(var); }
showvar();

Also note the case of default values.

module showvar(var=10) { ... } is nuanced as

module showvar() {
var = ( ( var == undef ) ? 10 : var );
echo(var);
}


Newly minted Admin - PM me if you need anything, or if I've done something stupid...

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!

View this message in context: http://forum.openscad.org/Special-Variables-tp14477p14656.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

L Boyd wrote > For example > var = 5; > module showvar() echo(var); > showvar(); // ECHO: 5 > showvar(var = 10); // ECHO: 10 > > showvar(var = 10); is equivalent to: > module showvar(){ var = 10; echo(var); } > showvar(); Also note the case of default values. > module showvar(var=10) { ... } is nuanced as > > module showvar() { > var = ( ( var == undef ) ? 10 : var ); > echo(var); > } ----- Newly minted Admin - PM me if you need anything, or if I've done something stupid... 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! -- View this message in context: http://forum.openscad.org/Special-Variables-tp14477p14656.html Sent from the OpenSCAD mailing list archive at Nabble.com.