Elisp function return value

12,152

First, you need an argument list after tmr-active-timer-p; the defun syntax is

(defun function-name (arg1 arg2 ...) code...)

Second, you do not need to wrap the body in progn.

Third, the return value is the last form evaluated. If your case you can just write

(defun tmr-active-timer-p ()
  "Returns t or nil depending of if there's an active timer."
  (when (file-exists-p tmr-file)
      ; (... more code)
    ))

then it will return nil if the file does not exist (because (when foo bar) is the same as (if foo (progn bar) nil)).

Finally, hanging parentheses are considered to be a poor code formatting style in lisp.

PS. Emacs Lisp does not have return, but it does have Nonlocal Exits. I urge you to avoid them unless you really know what you are doing.

Share:
12,152
hhaamm
Author by

hhaamm

Updated on June 04, 2022

Comments

  • hhaamm
    hhaamm almost 2 years

    I'm having a (probably) dumb problem with Elisp. I want a function to return t or nil depending on a when condition. This is the code:

    (defun tmr-active-timer-p
      "Returns t or nil depending of if there's an active timer"
      (progn
        (if (not (file-exists-p tmr-file))
            nil
          ; (... more code)
          )
        )
    )
    

    But I'm having an error. I'm not sure how to make a function return a value... I've read a function return the last expression result value but in this case, I wan't to make something like (PHP mess warning):

    // code
    
    if ($condition) {
      return false;
    }
    
    // more code...
    

    Maybe I'm missing the point and functional programming doesn't allow this approach?

  • hhaamm
    hhaamm over 9 years
    Well, your answer was correct. Basically there is no return sintaxis. You cannot return easily from a middle of a function in Elisp and probably is wouldn't be a good idea because of the functional paradigm. Thanks for the advice with hanging parenthesis.