Illegal operation warnings

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Illegal operation warnings

Ronaldo
Warnings for illegal operations in the last release and previous candidates is heaven and hell. It is welcome when the illegal operation is an error or unintended because it avoids that undef values propagate and produce warnings elsewhere in the code. Before the introduction of these invaluable warnings, invalid expressions returned undef and, once the language is not typed, validity checks of mixing types in expressions could be done by checking the results of the expression for an undef value.

A simple example of a validity check I used before the release is the sum of a list of numbers, vectors or matrices.

function sum(v) =
    assert(is_list(v) && len(v)>0)
    let( s = _sum(v,v[0]*0))
    assert(!is_undef(s), "List /v/ is not addable")    s;

function _sum(v,_total,_i=0) = 
    _i>=len(v) || is_undef(_total) ? _total :
    _sum(v,_total+v[_i], _i+1);

The second assert tries to capture any illegal operation of adding a number to a non-number (or vectors to matrices) in order to produce an appropriate error message. But it fails for a simple list as:

echo(sum([1, "a"])); // produces a warning

In more complex cases, appropriate error messages may be masked by a much less informative warning that helps only if you are able to go into the complex code details, what may be hard if it was written by another person.

It is possible to devise an ad hoc solution to the code above to avoid the warning by checking the expression arguments before doing the computation of the possibly invalid expression but there is a myriad of cases of invalid operations to consider. 

I think we need a general built-in function that can be used to capture or suppress these warnings, perhaps a function no_warning() that returns its input expression value when it is a valid expression and undef otherwise. In the code above, the capture would be done with something like:

function _sum(v,_total,_i=0) = 
    _i>=len(v) || is_undef(_total) ? _total :
    _sum(v, no_warning(_total+v[_i]), _i+1);  

Surely, by using no_warning(), a coder forgoes the clues a warning may give. But for a careful experienced coder that built-in will allow safer validity checks and more enlightening error messages.

Sent from the OpenSCAD mailing list archive at Nabble.com.

_______________________________________________
OpenSCAD mailing list
To unsubscribe send an email to [hidden email]