Interactive or Loading?

Started by m i c h a e l, June 07, 2006, 08:05:42 PM

Previous topic - Next topic

m i c h a e l

#15
Quote from: "William"So there seems to be nothing equivalent to

if __FILE__ == $0

in newLISP.


Actually, I think Lutz's response to my question is in keeping with the spirit of newLISP, which is the answer, "Yes!" to the question, "Can newLISP do my favorite thing in language x?" You just need to figure out how to program it ;-) Of course, Lutz may take pity and bequeath the solution on you, and like an answered wish, there it is! And then something you thought was a special part of your former language becomes a few lines of newLisp, and you begin to wonder what the fuss was all about in the first place.



May the Schwartz be with you,



m i c h a e l

Lutz

#16
Quote
In Ruby, the main purpose of if __FILE__ == $0 is to enable a library file to determine if it is being loaded by another file or if it is being run as the main program.


In newLISP I wouldn't know about a situation where this is important. I guess this addresses a Ruby specific situation with multiple loading of libraries?



But you could have some kind of flag at the top-level of you module



(context 'MyLib)

(set 'loaded true)
...
...


The main program then could test for the 'MyLib:loaded' flag  to find out if it has already been loaded:



(if MyLib:loaded
   (do-this)
   (do-that))


In newLISP when loading a module multiple times, all the top-level define's and assignments in the module will simply be overwritten again to their initial state and without wasting any additional memory.



If this should be prevented then some thing like the above load-flag or a load counter could prevent this. Another method would be to let the loaded module itself detect if already loaded and then perhaps exit.



Lutz