Fix dictionary errors in OpenSCAD 2021
The unit tests for libdict.scad
were failing on OpenSCAD 2021, which was confusing and a bit sad. I think the solution is currently fixing the version of OpenSCAD to 2019.5 in the CI script, but this MR makes it work on 2021 (and 2019).
I have traced this down to the summation, in _check_errant_match
. The problem is that the increment condition (i=i+1, count_nl=count_nl+non_list[i]
) gets evaluated for i==len(non_list)
which raises a warning. I guess this warning is new in the latest release.
I have implemented two solutions to this:
- The first is simple but hacky: I wrap the indexing in the increment of
count_nl
with a ternary operator, i.e. the increment becomes:i=i+1, count_nl=count_nl + (i<len(matches) ? non_list[i] : 0)
- The second fix is longer but nicer: I replace the list comprehension with a tail-recursive function that sums the list. That will be internally optimised by OpenSCAD to a for loop, so the speed difference should be slim to none. That's the final commit on this branch and is the solution I suggest we use.