ruricolist / serapeum

Utilities beyond Alexandria
MIT License
415 stars 41 forks source link

Consider adding if-not, if-not-let #172

Closed kilianmh closed 1 month ago

kilianmh commented 1 month ago

While using if-let I want the then clause to execute when one or all variables evaluate to nil. Currently with if-let that is not possible. To achieve that we can copy the code from if-let in alexandra and simply change this line from

(if (and ,@variables)

to

(if (not (and ,@variables))

Do you like the idea of adding this macro? If yes, then the name could also be not-if-let, if-not-let, or let-if-not.

ruricolist commented 1 month ago

If we add this, I think if should probably be as if-not-let, and we should probably also add if-not.

Do you have any examples of a situation where this macro could be useful? Nothing comes to mind for me.

kilianmh commented 1 month ago

Do you have any examples of a situation where this macro could be useful?

Use case:

Here dummy code to showcase the difference if-let vs if-not-let:

(if-let
  (variable-1 initial-form-1)
  (
  .
  .
  .
  .
  .
  )
  (error-resulting-from-variable-1-nil))

And here written with if-not-let

(if-not-let
  (variable-1 initial-form-1)
  (error-resulting-from-variable-1-nil)
  (
  .
  .
  .
  .
  .
  ))

With if-not-let, the error is next to the code that caused it. Especially with multiple nested if-let forms of this structure the code is more readable when replaced by if-not-let.

we should probably also add if-not

I guess, yes.

ruricolist commented 1 month ago

Interesting. I actually hadn't realized if-let bindings were in effect in the else clause. In that case I think the idea makes sense if you want to go ahead with a PR for if-not and if-not-let.