7

Given a string, built by various concatenation, "my-func-name", I would like to call the associated function.

As funcall expects a function object as parameter, I would like to know if there is a way to retrieve the function reference by its name, so I can execute it.

Hint: I am currently using the Emacs Lisp dialect.

Thank you very much

Bonus: Sample dummy code

(defun my-func-name ()
  "My function."
  (message "Hello"))

(setq mfname "my-func-name")

;; Not working, obviously
;; (funcall mfname)

1 Answer 1

12

Get the symbol of that name with intern, and then funcall it:

(funcall (intern "my-func-name"))
Sign up to request clarification or add additional context in comments.

3 Comments

and if (setq fname = "myfunc") how to call fname?
@RichieHH That would be (funcall (intern fname)).
Thanks for the reply. Silly me. I put the Q wrong. But I'm sorted now I actually needed (funcall (symbol-val (intern-soft myVarHoldingFunctionNameAsString))). Though looking at that I need to go play in scratch again. One day the whole string/symbol thing will click...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.