[OpenSCAD] assertion is ignored

adrianv avm4 at cornell.edu
Sun Nov 17 20:39:10 EST 2019


Is this a bug?   I have run across this sort of problem before, but in more
complex situations.  This one is quite simple.  

function lcm(a,b) =
  assert(is_num(a) && is_num(b),"Invalid parameters to lcm")
  abs(a*b) / gcd(a,b);

function gcd(a,b) =
   b==0 ? abs(a) : gcd(b,a % b);

echo(lcm([60,12]));   // Oops, called with list instead of two args

Result:

ERROR: Recursion detected calling function 'gcd' in file test105.scad, line
159
TRACE: called by 'gcd', in file test105.scad, line 159.
TRACE: called by 'gcd', in file test105.scad, line 159.
TRACE: called by 'gcd', in file test105.scad, line 159.
TRACE: called by 'gcd', in file test105.scad, line 159.
TRACE: called by 'gcd', in file test105.scad, line 159.

If I rewrite the code like this:

function lcm(a,b) =
  let(
    parmok = is_num(a) && is_num(b),
    dummy=assert(parmok,"Invalid parameters to lcm")
  )
  abs(a*b) / gcd(a,b);

function gcd(a,b) =
   b==0 ? abs(a) : gcd(b,a % b);

echo(lcm([60,12]));   // Oops, called with list instead of two args

Then I get the expected behavior:

ERROR: Assertion 'parmok' failed: "Invalid parameters to lcm" in file
test105.scad, line 145
TRACE: called by 'lcm', in file test105.scad, line 152.
TRACE: called by 'echo', in file test105.scad, line 152.





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



More information about the Discuss mailing list