I don't know if it is possible of not, but is there a way to overload Modules and/or Functions in OpenSCAD?
Nathan Sokalski
njsokalski@hotmail.commailto:njsokalski@hotmail.com
I can't see how you can do that without a static type system. OpenScad is
sort of dynamically typed with no explicit type declarations.
On Sat, 26 Aug 2023 at 19:21, Nathan Sokalski njsokalski@hotmail.com
wrote:
I don't know if it is possible of not, but is there a way to overload
Modules and/or Functions in OpenSCAD?
Nathan Sokalski
njsokalski@hotmail.com
OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org
As I noted earlier, more specific questions are better. What do you mean
by "overloading"? What exactly is the thing you want to be able to do?
With dynamic typing you can choose behavior at run time based on type, so
it's not clear what overloading means.
Example of changing behavior based on type:
So something like
function f(x) =
is_string(x) ? <do something>
: is_num(x) ? <do something else>
: is_list(x) ? <do a third thing>
: <catch all case>;
You could also make a module or function that uses named arguments to do
different things, like
function f(a,b) =
assert(is_undef(a) || is_undef(b)) // can't give a and b together
!is_undef(a) ? <do something for the A mode of operation>
!is_undef(b) ? <do something for the B mode of operation>
: assert(false,"must give either a or be");
Then f(a=value) does one thing and f(b=value) does something different.
On Sat, Aug 26, 2023 at 2:21 PM Nathan Sokalski njsokalski@hotmail.com
wrote:
I don't know if it is possible of not, but is there a way to overload
Modules and/or Functions in OpenSCAD?
Nathan Sokalski
njsokalski@hotmail.com
OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org
On Aug 26, 2023, at 11:20 AM, Nathan Sokalski njsokalski@hotmail.com wrote:
I don't know if it is possible of not, but is there a way to overload Modules and/or Functions in OpenSCAD?
If by that you mean "How do you override built-in modules/functions in openscad to add functionality?", I do it something like this (over-simplified for clarity):
File1: builtins.scad
module _circle(r,d) circle(r=r,d=d);
File2:
use <builtins.scad>
module circle(r,d,circum=false) {
r = is_num(r)? r : d/2;
_circle(r=r/(circum?cos(180/$fn):1));
}
-Revar
I guess I am just being lazy in a way, since I am aware that the type-testing functions could be used in most cases. For example, I am aware that I could use the type-testing functions to convert the following to an overload:
function ScalePoints(factor=1,points=[[0,0]])=ScalePoints(factor=[factor,factor],points=points);
function ScalePoints(factor=[1,1],points=[[0,0]])=[for(i=[0:(len(points)-1)])[factor.xpoints[i].x,factor.ypoints[i].y]];
However, adding type-testing can sometimes make the code somewhat ugly. But the hardest scenario is when the difference is the number of parameters rather than the type. I am aware that because OpenSCAD requires named arguments anyway, you could just test whether a parameter has the default value, but once again, this can make the code somewhat ugly. I think my real question is whether or not there is any way to define the signatures separately to make the code easier to read.
Nathan Sokalski
njsokalski@hotmail.commailto:njsokalski@hotmail.com
From: Revar Desmera revarbat@gmail.com
Sent: Sunday, August 27, 2023 3:26 AM
To: OpenSCAD general discussion discuss@lists.openscad.org
Subject: [OpenSCAD] Re: Module & Function Overloading In OpenSCAD
On Aug 26, 2023, at 11:20 AM, Nathan Sokalski njsokalski@hotmail.com wrote:
I don't know if it is possible of not, but is there a way to overload Modules and/or Functions in OpenSCAD?
If by that you mean "How do you override built-in modules/functions in openscad to add functionality?", I do it something like this (over-simplified for clarity):
File1: builtins.scad
module _circle(r,d) circle(r=r,d=d);
File2:
use <builtins.scad>
module circle(r,d,circum=false) {
r = is_num(r)? r : d/2;
_circle(r=r/(circum?cos(180/$fn):1));
}
-Revar
On 8/27/2023 8:48 AM, Nathan Sokalski wrote:
I think my real question is whether or not there is any way to define
the signatures separately to make the code easier to read.
No.