Programmatically define function?

Hello,

I am trying to solve bit of annoyance and easiest solution would be if I could use variable to control, if a specially named function is defined. Preferably if it is defined or not, something like the hook functions, since creative users of my package could be using this function for their own purpose - I do not want to hog the name permanently. The trouble is, that I would like to be able to also undefine it, if needed.  

Something like (this does not work...):

variable testme=1

#if(testme)

    Function MyHook(wv)
        Wave wv
        MatrixOp/O wv=wv^t
    end
#endif

Is there some way of doing this?  

Compile-time statements like the one in your example do not work well in general for such things. You should tell us a bit more what you want to do. Depending on your specific use case, you might be able to achieve something similar using FUNCREF. This can be, for example, used to assigns a function from a string. For example, if your variable is not set the function call is redirected to a dummy function which does nothing, but if set it goes to MyHook(). You can read more here:

DisplayHelpTopic "Function References"

 

Thanks. I was aware of the FUNCREF but 10+ years ago I was not smart enough to design the code to use this. 

Well, I wrote code which can create a new function and recompile procedures; removing this function would probably be also doable. But more and more I look at it, this is such ugly hack that I will find different solution. Let's forget about this ;-)  I will, carefully, revise the old code. 

Yep- you can programmatically write code into a plain-text notebook, save it to a file, then use Execute/P to #include it and compile.

That process is fraught with difficulty and fragility. I know this personally- that's how the Function Grapher package (which I wrote) works.

And in case you are using IP9 you can use Execute/P with "RELOAD CHANGED PROCS". This allows to forcecfully recompile everyting in IP, especially helpful if you rewrote procedure code behinds Igor back before. John added that for us ;)

Yep. So another way to do what you want is to use some external scriptable text editor to change procedure files that are currently open in Igor, and then use Thomas' Execute/P method to recompile. If you do that, we will try to help you when you have troubles, but not very hard :) You may wind up wandering in the woods...