discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Using children with let

JS
John Savage
Wed, Sep 12, 2018 2:06 PM

I would like to be able to write a single let() statement and use it on
multiple modules. A simple example is

module leta(){

let(a=23)

  children();

}

leta() cylinder(h=a, d=a); // Doesn't work, a is unknown here.

However this does work:

module move(x){

translate([x,0,0])

  children();

}

module cone_rod(){

cylinder(h=10,d=5);

cylinder(h=5,d1=15,d2=0);

}

move(0) cone_rod();      // Works as expected

move(20) cone_rod();    // Works as expected

What's the difference and why doesn't the first example work?

Thanks,

John

I would like to be able to write a single let() statement and use it on multiple modules. A simple example is module leta(){ let(a=23) children(); } leta() cylinder(h=a, d=a); // Doesn't work, a is unknown here. However this does work: module move(x){ translate([x,0,0]) children(); } module cone_rod(){ cylinder(h=10,d=5); cylinder(h=5,d1=15,d2=0); } move(0) cone_rod(); // Works as expected move(20) cone_rod(); // Works as expected What's the difference and why doesn't the first example work? Thanks, John
NH
nop head
Wed, Sep 12, 2018 3:05 PM

OpenSCAD variables have lexical scope by default, so they can only be seen
in the block they are defined in. So you could do children(a) to access the
23rd child but a is not visible outside leta() and more specifically only
under the let().

However, if you use $a it will have dynamic scope and be accessible in all
the modules and functions called from the block it is defined in.

On 12 September 2018 at 15:06, John Savage john@jegsav.com wrote:

I would like to be able to write a single let() statement and use it on
multiple modules. A simple example is

module leta(){

let(a=23)

   children();

}

leta() cylinder(h=a, d=a); // Doesn't work, a is unknown here.

However this does work:

module move(x){

translate([x,0,0])

   children();

}

module cone_rod(){

cylinder(h=10,d=5);

cylinder(h=5,d1=15,d2=0);

}

move(0) cone_rod();      // Works as expected

move(20) cone_rod();    // Works as expected

What’s the difference and why doesn’t the first example work?

Thanks,

John


OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

OpenSCAD variables have lexical scope by default, so they can only be seen in the block they are defined in. So you could do children(a) to access the 23rd child but a is not visible outside leta() and more specifically only under the let(). However, if you use $a it will have dynamic scope and be accessible in all the modules and functions called from the block it is defined in. On 12 September 2018 at 15:06, John Savage <john@jegsav.com> wrote: > I would like to be able to write a single let() statement and use it on > multiple modules. A simple example is > > > > module leta(){ > > let(a=23) > > children(); > > } > > > > leta() cylinder(h=a, d=a); // Doesn't work, a is unknown here. > > > > However this does work: > > > > module move(x){ > > translate([x,0,0]) > > children(); > > } > > > > module cone_rod(){ > > cylinder(h=10,d=5); > > cylinder(h=5,d1=15,d2=0); > > } > > > > move(0) cone_rod(); // Works as expected > > move(20) cone_rod(); // Works as expected > > > > What’s the difference and why doesn’t the first example work? > > Thanks, > > John > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org > >
JS
John Savage
Wed, Sep 12, 2018 5:30 PM

Hi Nop Head(?) Thanks for your reply. I get it now. John

From: nop head [mailto:nop.head@gmail.com]
Sent: 12 Sep 2018 04:05
To: john@jegsav.com; OpenSCAD general discussion
Subject: Re: [OpenSCAD] Using children with let

OpenSCAD variables have lexical scope by default, so they can only be seen in the block they are defined in. So you could do children(a) to access the 23rd child but a is not visible outside leta() and more specifically only under the let().

However, if you use $a it will have dynamic scope and be accessible in all the modules and functions called from the block it is defined in.

On 12 September 2018 at 15:06, John Savage john@jegsav.com wrote:

I would like to be able to write a single let() statement and use it on multiple modules. A simple example is

module leta(){

let(a=23)

  children();

}

leta() cylinder(h=a, d=a); // Doesn't work, a is unknown here.

However this does work:

module move(x){

translate([x,0,0])

  children();

}

module cone_rod(){

cylinder(h=10,d=5);

cylinder(h=5,d1=15,d2=0);

}

move(0) cone_rod();      // Works as expected

move(20) cone_rod();    // Works as expected

What’s the difference and why doesn’t the first example work?

Thanks,

John

Image removed by sender.


OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

Hi Nop Head(?) Thanks for your reply. I get it now. John From: nop head [mailto:nop.head@gmail.com] Sent: 12 Sep 2018 04:05 To: john@jegsav.com; OpenSCAD general discussion Subject: Re: [OpenSCAD] Using children with let OpenSCAD variables have lexical scope by default, so they can only be seen in the block they are defined in. So you could do children(a) to access the 23rd child but a is not visible outside leta() and more specifically only under the let(). However, if you use $a it will have dynamic scope and be accessible in all the modules and functions called from the block it is defined in. On 12 September 2018 at 15:06, John Savage <john@jegsav.com> wrote: I would like to be able to write a single let() statement and use it on multiple modules. A simple example is module leta(){ let(a=23) children(); } leta() cylinder(h=a, d=a); // Doesn't work, a is unknown here. However this does work: module move(x){ translate([x,0,0]) children(); } module cone_rod(){ cylinder(h=10,d=5); cylinder(h=5,d1=15,d2=0); } move(0) cone_rod(); // Works as expected move(20) cone_rod(); // Works as expected What’s the difference and why doesn’t the first example work? Thanks, John Image removed by sender. _______________________________________________ OpenSCAD mailing list Discuss@lists.openscad.org http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org