typesopenscad

Data type of a range expression in OpenSCAD


In OpenSCAD, a for loop can specify a range in the form [first:step:last], where step: is optional. It seems you can also assign a range expression to a variable.

range = [1:10];

for (i = range) echo(i); // works as expected

// But the range expression is _not_ a special form of list
// comprehension because `range` is not a list:

echo(range);  // prints [ 1 : 1 : 10 ]
echo(range[0], range[1], range[2], range[3]); // prints 1, 1, 10, undef
echo(is_list(range));  // prints false

// Also, this results in an error:
echo(len(range));

There are times it would be useful for a function or module to have a range parameter instead of requiring two or three individual numeric arguments to achieve the same effect.

Does a range expression have a specific data type or is this an artifact of the implementation? If there is a specific data type, is there a way (other than process of elimination) for function to test whether a parameter has been bound to a range expression?


Solution

  • A range expression doesn't have a specific data type.

    A range value is indexable. Its elements are numbers but it's not a list.

    function is_range(x) = is_num(x[0]) && !is_list(x);