jeudi 2 avril 2020

Languages with an "else-less if" construct?

For the longest time, when using Emacs-lisp I was wondering about the purpose of the when and unless constructs. To me it seemed, that it is merely a workaround for nicer indentation and reduced nesting, i.e.

(if (this-is-true)     (when (this-is-true)  | (if (not (this-is-true))     (unless (this-is-true)
    (progn         --\   (do-this)           |     (progn               --\   (do-this)
      (do-this)    --/   (do-that))          |       (do-this)          --/   (do-that))
      (do-that)))                            |       (do-that)))

However, now that I am working professionally as a programmer and thus more often with other people's code, I have often seen situations, where an

// ALGOL style curly-braces       ! Fortran            # Shell scripts
if(this) {                        IF(THIS) THEN        if this_condition; then
    then_than();                      DO_THAT              do_that
    ...                               ...                  ...

made me scramble to see, whether there are else or if-else clauses. Similar with switch or case style statements, and whether there is a default clause, demonstrating a real cognitve advantage of the more constrained when and unless forms over using a plain if, i.e. they improve the expressiveness.

This made me wonder: What other languages outside of lisp variants exist, that have a builtin notion of an "else-less" if? Are there maybe even languages that try to enforce using different constructs, e.g. by not allowing an if without an else?

Aucun commentaire:

Enregistrer un commentaire