? :
我目前这样做:
for (int index = 0; index < alpha.length; index++) {
alpha[index] = index * ((index < 25) ? index : 3);
}
对函数名称进行硬编码似乎不是一个好习惯。
有关实施{
"_index": "people",
"_type": "employee",
"_id": "8725",
"_source": {
"id": 43470,
"firstName": "John",
"lastName": "Smith",
"groups": [
{
"id": 345,
"name": "Developers"
},
{
"id": 75432,
"name": "Scrummasters"
},
{
"id": 5789,
"name": "UX"
}
]
}
},
{
"_index": "people",
"_type": "employee",
"_id": "8726",
"_source": {
"id": 43471,
"firstName": "Fred",
"lastName": "Bloggs",
"groups": [
{
"id": 474,
"name": "Developers"
},
{
"id": 824,
"name": "Admins"
}
]
}
}
或(defun foo ()
(send-to-debug-log "Error. Function terminated." (get-current-function-name)))
。
答案 0 :(得分:3)
{{1}}
答案 1 :(得分:2)
使用包which-function-mode
(在melpa中),您可以以编程方式调用函数
(Which-function)
更多信息:
答案 2 :(得分:1)
这里似乎运作得很好:
(defun calling-function ()
(let ((n 6) ;; nestings in this function + 1 to get out of it
func
bt)
(while (and (setq bt (backtrace-frame n))
(not func))
(setq n (1+ n)
func (and bt
(nth 0 bt)
(nth 1 bt))))
func))
如果你在lambda中调用它,你就得到了整个lambda。它适用于apply
,funcall
和eval
。
我认为最简单,最健壮的方法就是明确地编写函数名称,正如您现在所做的那样。
答案 3 :(得分:1)
您可以尝试以下方式:
(defconst my-defun-macros '(defun cl-defun defmacro cl-defmacro))
(defun my-add-defun-name (orig-fun name &rest args)
(let* ((my-current-defun-name (if (consp name) (cadr name) name))
(macroexpand-all-environment
(cons (cons 'my-get-current-function-name
(lambda () my-current-defun-name))
macroexpand-all-environment))
(code (apply orig-fun name args)))
(macroexpand-all code macroexpand-all-environment)))
(defmacro my-get-current-function-name ()
"Return current defun's name.
Only works within the advised macros in `my-defun-macros'."
(error "my-get-current-function-name used outside of a defun"))
(dolist (m my-defun-macros)
(advice-add m :around #'my-add-defun-name))