Code Red > AutoLISP (Vanilla / Visual)

Nested defun's

(1/3) > >>

Mark:
How does everyone feel about nested defun's? I don't see many hackers using them in their lisp programs. I'm of the opinion that, if you write a program for a client you would want to isolate all your functions. This of course would minimize the chance of conflicts.

Questions:
1. Are there any draw backs to using nested defun's?
2. Are there any benefits to using nested defun's?

Kerry:
Hi Mark

I use them regularly, and have for ages [literally] ;)

The only percieved drawback I am aware of could be the length of the programme. I sometimes break up longer apps temporarily to handle this.

Benefits.
Keeps code tight.
I use a lot with DCL {or objectDCL} control functions.
Variables can be semi-global ie: declared in the head function only.

There is an issue with the code-check function in the VLide not recognising some of the variables as local/global during the Statistics report in Verbal mode, but this is only cosmetic.

Here is an example from ExpressoCodeCafe Thingies  
http://www.vbdesign.net/expresso/showthread.php?s=&threadid=5225
Regards
Kerry

added: The function name conflict can be an issue. I use registered prefix names which does reduce the risk. I also protect assign my functions.

JohnK:
I dont do that normaly. I dont know why, I guess my progns never end up that way.  It would be "neater" though wouldnt it?  Is there any solid reason for using a nested defun?  Would that be the equlivant of making your variables private?

Mark:

--- Quote from: Se7en ---Would that be the equlivant of making your variables private?
--- End quote ---

Yes. Your functions would not be visible if the user simply loaded the program file.

Mark:
Here's an example, load this and try to run (fun 3). You'll get "no function definition: FUN"


--- Code: ---(defun add2 (/ fun rn)
     (defun fun (n) (+ n 100))

     (if
       (setq rn (getreal "\nEnter a number: "))
       (fun rn)
       )
     )
--- End code ---

Navigation

[0] Message Index

[#] Next page

Go to full version