STklos icon indicating copy to clipboard operation
STklos copied to clipboard

argument checking: use "assume" or always call error?

Open jpellegrini opened this issue 2 years ago • 2 comments

Hi @egallesio !

Some SRFIs use assume in arguments checking (like SRFI-214, flexvectors). Others always raise an error. Maybe this should be standardized? Either always one of them, or always the other? I can slowly change the SRFIs to always use assume if you think this is OK.

jpellegrini avatar Jan 10 '22 14:01 jpellegrini

The pro of assume is that the test is not done if you are not in debug mode (it's expansion is #void, so it's fast :smile:). The con is that if the user doesn't set the debug flag, its code could fail miserably. In this way, its very similar to the C assert function.

IMHO, assume should be used for internal functions to assume that a certain property is true at a given point, but if the test involves a data given by the user error is better. So both can probably cohabit in a program.

For instance:

(define (public-foo n)
  (unless (integer? n)
    (error "bad integer" n))
  (internal-bar (* n n)))

(define (internal-bar n)
  (assume (positive? n))
  .....
  .....)

This is just my opinion, conforming of what STklos is able to do with assume (SRFI 145 depicts situations the compiler cannot handle).

egallesio avatar Jan 17 '22 18:01 egallesio

Ok, I'll review some of the SRFIs later and see if we need to change anything

jpellegrini avatar Jan 17 '22 21:01 jpellegrini