From: Joakim Hove
Subject: Adding functions at runtime - understanding eval
Date: 
Message-ID: <4y4r6khk9k.fsf@skjellgran.ii.uib.no>
<disclaimer> 
This is related to emacs-lisp, I hope and think the question is
sufficently general to be asked here, but if I am really asking about
some emacs idiosyncrasies please forgive me.
</disclaimer>

I have the following code:

  (defun create-function (defun-string)
    (eval defun-string))

  (defun test-it ()
    (create-function "(defun dynamic-function (arglist) (list 25))")
    (message "Dynamic function gives %s" (dynamic-function nil)))


Basically I have written a full function definition in a string, and
then eval this string. However when I subsequently try to call the
newly created function "dynamic-function" I just get the error:
"Symbol's function definition is void: dynamic function", so the eval
step has not worked as I thought.


Any suggestions greatly appreciated

Joakim Hove


-- 
Joakim Hove  / ····@ii.uib.no  /  (55 5) 84076

From: Espen Vestre
Subject: Re: Adding functions at runtime - understanding eval
Date: 
Message-ID: <kwr89oyetc.fsf@merced.netfonds.no>
Joakim Hove <····@ii.uib.no> writes:

> This is related to emacs-lisp, I hope and think the question is
> sufficently general to be asked here, but if I am really asking about
> some emacs idiosyncrasies please forgive me.

It's general enough. The problem with your code is that the argument
to eval should be a _list_, not a _string_ (there's nothing wrong with
giving eval a string, but you will only get the same string back). 

This will work:

  (defun test-it ()
    (create-function '(defun dynamic-function (arglist) (list 25)))
    (message "Dynamic function gives %s" (dynamic-function nil)))

(if the input data really is a string, use read-from-string to produce
the form)
-- 
  (espen)
From: Joakim Hove
Subject: Re: Adding functions at runtime - understanding eval
Date: 
Message-ID: <4ysmu4g45d.fsf@skjellgran.ii.uib.no>
Thanks for your suggestion,

> This will work:
>
>   (defun test-it ()
>     (create-function '(defun dynamic-function (arglist) (list 25)))
>     (message "Dynamic function gives %s" (dynamic-function nil)))
>

It absolutely worked.


> (if the input data really is a string, use read-from-string to
>produce the form) -- (espen)

The input data was indeed a string, dynamically built during
execution, so the tip about (read-from-string xxx) was essential.

Thanks again - Joakim

-- 
Joakim Hove  / ····@ii.uib.no  /  (55 5) 84076
From: Kenny Tilton
Subject: Re: Adding functions at runtime - understanding eval
Date: 
Message-ID: <3E639FD7.3080601@nyc.rr.com>
Joakim Hove wrote:

> The input data was indeed a string, dynamically built during
> execution, 

I do not know how strictly you meant the above, but FWIW "built during 
execution" does not necessarily mean "a string"; you could just as well 
be building up a tree of symbols. As Matthew wrote:

> (list 'defun 'dynamic-function (list 'arglist) (list 'list 25))


-- 

  kenny tilton
  clinisys, inc
  http://www.tilton-technology.com/
  ---------------------------------------------------------------
"Cells let us walk, talk, think, make love and realize
  the bath water is cold." -- Lorraine Lee Cudmore