Variable scope - undocumented feature ?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Variable scope - undocumented feature ?

Guenther Sohler-2

I have written this funciton in OpensCAD 2019.05:


module build(s,ndst)
{
            thing(s,$nsrc,ndst,port)
            union()
            {
                $nsrc=ndst;
                children();
            }
}

Watch the custom $nsrc variable.
Its set to a certain value at the beginning. But it can be redefined for the inner scope
when children() is executed.

The fact that variables are set at  compile time and only the latest value set holds is no longer true.
Without using the $ sign, this does not work anymore

cheers, Günther


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Variable scope - undocumented feature ?

nophead
Variables that start with a $ have dynamic scope and are visible in all the called functions and modules and children. Normal variables have lexical scope and are visible in the textually nested blocks only.


On Wed, 19 Jun 2019 at 15:29, Guenther Sohler <[hidden email]> wrote:

I have written this funciton in OpensCAD 2019.05:


module build(s,ndst)
{
            thing(s,$nsrc,ndst,port)
            union()
            {
                $nsrc=ndst;
                children();
            }
}

Watch the custom $nsrc variable.
Its set to a certain value at the beginning. But it can be redefined for the inner scope
when children() is executed.

The fact that variables are set at  compile time and only the latest value set holds is no longer true.
Without using the $ sign, this does not work anymore

cheers, Günther

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org