LoginSignup
1
0

More than 5 years have passed since last update.

Elisp の変数の内容が無名関数かどうかチェックする

Last updated at Posted at 2012-06-13

変数が無名関数かどうかは funcall が invalid-function エラーを発生するかで確認できそう。

(defun lambda-p (var)
  (condition-case nil
      (progn
        (funcall var)
        t)
    (invalid-function nil)))


(setq str "hoge")
(lambda-p str)                          ; => nil

(setq lmd (lambda () (format "%s" "huga")))
(lambda-p lmd)                          ; => t

Elisp の変数が「文字列そのもの」と「文字列を返す関数」のどちらかに関わらず、その変数を文字列として扱いたい場合はこんな感じ。

(defun lambda-to-str (str)
  (condition-case nil
      (funcall str)
    (invalid-function str)))


(setq str "hoge")
(lambda-to-str str)                          ; => "hoge"

(setq lmd (lambda () (format "%s" "huga")))
(lambda-to-str lmd)                          ; => "huga"
1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0