discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

reference built-in names on overwrite

P
Parkinbot
Wed, Feb 20, 2019 1:02 PM

It was discussed before, but not in extension and afaik without a solution so
far. Sometimes I want to rewrite built-in modules or functions in order to
e.g. extend their parameter list and functionality. The problem: "after" and
within that code I loose access to the built-in symbols. So particularly
there is no way back if the rewrite happens within a lib.

Say I want to rewrite cube() in a lib to get rid of the cumbersome brackets
and default it to center=true, e.g. by:

cube(10, 20, 1);
module cube(size=1, y=1, z=1, center = true, x=undef)
{
x = is_undef(x)?size:x;
if (is_undef(x[0])) cube([x, y, z], center=center); else cube(x, center);
}

This obviously results in a recursion error. So I have to use a naming
convention:

module cube_(size=1, y=1, z=1, center = true, x=undef)
{
x = is_undef(x)?size:x;
if (is_undef(x[0])) cube([x, y, z], center=center); else cube(x, center);
}

I'd rather like to write something like
module cube(size=1, y=1, z=1, center = true, x=undef)
{
x = is_undef(x)?size:x;
if (is_undef(x[0])) $cube([x, y, z], center=center); else $cube(x,
center);
}

and then if necessary be able to switch back to the built-in version by

module cube(size=1, center=false)  $cube(size, center);

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

It was discussed before, but not in extension and afaik without a solution so far. Sometimes I want to rewrite built-in modules or functions in order to e.g. extend their parameter list and functionality. The problem: "after" and within that code I loose access to the built-in symbols. So particularly there is no way back if the rewrite happens within a lib. Say I want to rewrite cube() in a lib to get rid of the cumbersome brackets and default it to center=true, e.g. by: cube(10, 20, 1); module cube(size=1, y=1, z=1, center = true, x=undef) { x = is_undef(x)?size:x; if (is_undef(x[0])) cube([x, y, z], center=center); else cube(x, center); } This obviously results in a recursion error. So I have to use a naming convention: module cube_(size=1, y=1, z=1, center = true, x=undef) { x = is_undef(x)?size:x; if (is_undef(x[0])) cube([x, y, z], center=center); else cube(x, center); } I'd rather like to write something like module cube(size=1, y=1, z=1, center = true, x=undef) { x = is_undef(x)?size:x; if (is_undef(x[0])) $cube([x, y, z], center=center); else $cube(x, center); } and then if necessary be able to switch back to the built-in version by module cube(size=1, center=false) $cube(size, center); -- Sent from: http://forum.openscad.org/
M
MichaelAtOz
Wed, Feb 20, 2019 9:06 PM

+1
I too have desired this for some time.
There is one significant downside, that being supportability, if cube() no
longer behaves as it seemingly should, possibly due to something berried in
a library three layers deep, it is going to drive people insane...


Admin - email* me if you need anything, or if I've done something stupid...

  • click on my MichaelAtOz label, there is a link to email me.

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!

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

+1 I too have desired this for some time. There is one significant downside, that being supportability, if cube() no longer behaves as it seemingly should, possibly due to something berried in a library three layers deep, it is going to drive people insane... ----- Admin - email* me if you need anything, or if I've done something stupid... * click on my MichaelAtOz label, there is a link to email me. 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! -- Sent from: http://forum.openscad.org/
P
Parkinbot
Wed, Feb 20, 2019 10:37 PM

I wouldn't sign that default values and parameters for built-ins have been
established in a foresighted manner. A properly designed lib can take a lot
of clumsyness and burden from this and lead to a much richer standard lib.

BTW, current OpenSCAD does not prevent you from rewriting cube() in a lib
buried three layers deep. Your implementation just can't refer to the
built-in cube(). And there is no way back to built-in behaviour.

cube(10,20,30);
module cube(x,y,z, center = true) linear_extrude (z, center = center)
square([x,y], center = center);

MichaelAtOz wrote

There is one significant downside, that being supportability, if cube() no
longer behaves as it seemingly should, possibly due to something buried in
a library three layers deep, it is going to drive people insane...

I wouldn't sign that default values and parameters for built-ins have been established in a foresighted manner. A properly designed lib can take a lot of clumsyness and burden from this and lead to a much richer standard lib. BTW, current OpenSCAD does not prevent you from rewriting cube() in a lib buried three layers deep. Your implementation just can't refer to the built-in cube(). And there is no way back to built-in behaviour. cube(10,20,30); module cube(x,y,z, center = true) linear_extrude (z, center = center) square([x,y], center = center); MichaelAtOz wrote > There is one significant downside, that being supportability, if cube() no > longer behaves as it seemingly should, possibly due to something buried in > a library three layers deep, it is going to drive people insane... -- Sent from: http://forum.openscad.org/