Syntax suggestion

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

Syntax suggestion

Troberg
It would be nice if we could take modules as arguments to other modules, a
little bit like children(). Something like this:

 module stamp(handleshape,stampshape,faceshape){
  union(){
  handleshape();
  difference(){
  stampshape();
  faceshape();
  }
  }
 }
 
 module myhandleshape(length){
  //Define handle here
 }
 
 module mylefthandedhandleshape(length){
  //Define handle here
 }
 
 module mystampshape(){
  //Define the shape of the rubber stamp outline here
 }
 
 module myfaceshape(){
  //Define the shape of the stamp cutouts here
 }
 
 module mylogofaceshape(){
  //Define the shape of the stamp cutouts here
 }
 
 stamp(myhandleshape(100),mystampshape,myfaceshape);
 stamp(mylefthandledhandleshape(100),mystampshape,mylogofaceshape);

This is just a simple example, and I know this example could have been done
differently, in just a little more awkward ways. But, as things get more
complicated, it can really make the code much neater and more modular and
flexible.



--
Sent from: http://forum.openscad.org/

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

Re: Syntax suggestion

nophead
The problem is there may also be a function called handleshape() and because functions and variables have separate namespaces  handleshape() means call that function, not interpret a variable with the same name as a function reference,

On Tue, 26 Feb 2019 at 13:34, Troberg <[hidden email]> wrote:
It would be nice if we could take modules as arguments to other modules, a
little bit like children(). Something like this:

 module stamp(handleshape,stampshape,faceshape){
        union(){
                handleshape();
                difference(){
                        stampshape();
                        faceshape();
                }
        }
 }

 module myhandleshape(length){
        //Define handle here
 }

 module mylefthandedhandleshape(length){
        //Define handle here
 }

 module mystampshape(){
        //Define the shape of the rubber stamp outline here
 }

 module myfaceshape(){
        //Define the shape of the stamp cutouts here
 }

 module mylogofaceshape(){
        //Define the shape of the stamp cutouts here
 }

 stamp(myhandleshape(100),mystampshape,myfaceshape);
 stamp(mylefthandledhandleshape(100),mystampshape,mylogofaceshape);

This is just a simple example, and I know this example could have been done
differently, in just a little more awkward ways. But, as things get more
complicated, it can really make the code much neater and more modular and
flexible.



--
Sent from: http://forum.openscad.org/

_______________________________________________
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
Reply | Threaded
Open this post in threaded view
|

Re: Syntax suggestion

Troberg
nophead wrote
> The problem is there may also be a function called handleshape() and
> because functions and variables have separate namespaces  handleshape()
> means call that function, not interpret a variable with the same name as a
> function reference,

Well, that goes for children() as well.

But, that could probably be cleared with some variation of the syntax. It's
just an example. The point is to be able to send objects in as arguments.



--
Sent from: http://forum.openscad.org/

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

Re: Syntax suggestion

nophead
Not sure what you mean because children() only takes modules and they have their own namespace as well.

It's unusual to have different name spaces but it like it as I don't have to think up three names. I have screw() as a module and often screw as a variable and possibly a function.

People have suggested being able to pass functions before, even some pull requests IIRC but they haven't been merged. It gets more complex when you consider scope. Possible it needs a new variable type to hold a function reference. A new syntax to assign a function to that variable and a new syntax to call it.

On Tue, 26 Feb 2019 at 14:29, Troberg <[hidden email]> wrote:
nophead wrote
> The problem is there may also be a function called handleshape() and
> because functions and variables have separate namespaces  handleshape()
> means call that function, not interpret a variable with the same name as a
> function reference,

Well, that goes for children() as well.

But, that could probably be cleared with some variation of the syntax. It's
just an example. The point is to be able to send objects in as arguments.



--
Sent from: http://forum.openscad.org/

_______________________________________________
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
Reply | Threaded
Open this post in threaded view
|

Re: Syntax suggestion

JordanBrown
In reply to this post by Troberg
On 2/26/2019 5:29 AM, Troberg wrote:
It would be nice if we could take modules as arguments to other modules, a
little bit like children(). Something like this:

 module stamp(handleshape,stampshape,faceshape){
 	union(){
 		handleshape();
 		difference(){
 			stampshape();
 			faceshape();
 		}
 	}
 }
 
 module myhandleshape(length){
 	//Define handle here
 }
 
 module mylefthandedhandleshape(length){
 	//Define handle here
 }
 
 module mystampshape(){
 	//Define the shape of the rubber stamp outline here
 }
 
 module myfaceshape(){
 	//Define the shape of the stamp cutouts here
 }
 
 module mylogofaceshape(){
 	//Define the shape of the stamp cutouts here
 }
 
 stamp(myhandleshape(100),mystampshape,myfaceshape);
 stamp(mylefthandledhandleshape(100),mystampshape,mylogofaceshape);


If I understand what you're asking for, you can do this with children( ) today.

module stamp(){
	// Children:
        handle = 0;
	stamp = 1;
	face = 2;

	union(){
 		children(handle);
 		difference(){
 			children(stamp);
 			children(face);
 		}
 	}
 }
 
 module myhandleshape(length){
 	//Define handle here
 }
 
 module mylefthandedhandleshape(length){
 	//Define handle here
 }
 
 module mystampshape(){
 	//Define the shape of the rubber stamp outline here
 }
 
 module myfaceshape(){
 	//Define the shape of the stamp cutouts here
 }
 
 module mylogofaceshape(){
 	//Define the shape of the stamp cutouts here
 }
 
 stamp() {
     myhandleshape(100);
     mystampshape();
     myfaceshape();
 }
 stamp() {
     mylefthandledhandleshape(100);
     mystampshape();
     mylogofaceshape();
 }

One thing you can't do is pass arguments from the parent to the children, because what you're passing to the parent are objects, not function references.


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

Re: Syntax suggestion

nophead
You can pass arguments to children with $variables. 
module stamp($handle_size){
	// Children:
        handle = 0;
	stamp = 1;
	face = 2;

	union(){
 		children(handle);
 		difference(){
 			children(stamp);
 			children(face);
 		}
 	}
 }
stamp(100) {
     mylefthandledhandleshape($handle_size);
     mystampshape();
     mylogofaceshape();
 }


On Tue, 26 Feb 2019 at 19:49, Jordan Brown <[hidden email]> wrote:
On 2/26/2019 5:29 AM, Troberg wrote:
It would be nice if we could take modules as arguments to other modules, a
little bit like children(). Something like this:

 module stamp(handleshape,stampshape,faceshape){
 	union(){
 		handleshape();
 		difference(){
 			stampshape();
 			faceshape();
 		}
 	}
 }
 
 module myhandleshape(length){
 	//Define handle here
 }
 
 module mylefthandedhandleshape(length){
 	//Define handle here
 }
 
 module mystampshape(){
 	//Define the shape of the rubber stamp outline here
 }
 
 module myfaceshape(){
 	//Define the shape of the stamp cutouts here
 }
 
 module mylogofaceshape(){
 	//Define the shape of the stamp cutouts here
 }
 
 stamp(myhandleshape(100),mystampshape,myfaceshape);
 stamp(mylefthandledhandleshape(100),mystampshape,mylogofaceshape);


If I understand what you're asking for, you can do this with children( ) today.

module stamp(){
	// Children:
        handle = 0;
	stamp = 1;
	face = 2;

	union(){
 		children(handle);
 		difference(){
 			children(stamp);
 			children(face);
 		}
 	}
 }
 
 module myhandleshape(length){
 	//Define handle here
 }
 
 module mylefthandedhandleshape(length){
 	//Define handle here
 }
 
 module mystampshape(){
 	//Define the shape of the rubber stamp outline here
 }
 
 module myfaceshape(){
 	//Define the shape of the stamp cutouts here
 }
 
 module mylogofaceshape(){
 	//Define the shape of the stamp cutouts here
 }
 
 stamp() {
     myhandleshape(100);
     mystampshape();
     myfaceshape();
 }
 stamp() {
     mylefthandledhandleshape(100);
     mystampshape();
     mylogofaceshape();
 }

One thing you can't do is pass arguments from the parent to the children, because what you're passing to the parent are objects, not function references.

_______________________________________________
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
Reply | Threaded
Open this post in threaded view
|

Re: Syntax suggestion

JordanBrown
On 2/26/2019 11:56 AM, nop head wrote:
You can pass arguments to children with $variables.

Just when I thought I understood OpenSCAD... now my head has exploded.  Who's going to clean up the mess?

Thanks.  I don't know if I have any application for this mechanism, but it's always good when my mental model gets adjusted to be closer to reality.

So it sounds like we're awfully close to being able to do what Troberg asks, albeit in a way that's ... different ... from how it would be done in a more conventional language.



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

Re: Syntax suggestion

Troberg
In reply to this post by JordanBrown
Ooh, that's very neat, and takes care of the problem nicely. Thank you!



--
Sent from: http://forum.openscad.org/

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