Rethink how and when checkcode() is used
Originally, using checkcode() was introduced to work around the problem of engEvaluate hanging with incomplete input. Currently MEvaluate executes commands by writing them to a script, so checkcode is not needed for this purpose any more.
Proposal:
- remove
checkcodefromMEvaluateto reduce overhead - add
checkcodetoMScriptandMFunction: it should be run when a script is created - If possible, make
MFunctionverify that the file it creates is a true function and not just a script (is there a MATLAB function for this)?
Possible solution for distinguishing between scripts and functions so MFunction can verify that the code it was given truly describes a function: http://stackoverflow.com/a/15911256/695132
Something to pay attention to:
Consider
MEvaluate["clear x"]
MEvaluate["
x=1
foo
"]
and
MEvaluate["clear x"]
MEvaluate["
x=1
y=1+
"]
Will x have a value after these two? With the current implementation of MEvaluate, if will have a value after the first one, but not after the second one. The return value is $Failed for both cases.
This behaviour is rather confusing, and it would be beneficial if MATLink could give some indication whether any of the lines were evaluated at all. With the current implementation (using checkcode) this should be easy, but I am not sure if will still be easy if we remove checkcode.
Hmm... I don't think some of this is necessary.
- There is the undocumented
MEvaluate[..., "NoCheck"]for this, to reduce overhead. Besides, repeated runs of the same input should be in anMScript, which when called withMEvaluate, will not check for errors. - This will be added very soon (and is important, as errors in MScripts are not displayed right now — I wonder how that went unnoticed for so long).
- This is not necessary, because when you try to call a non function as a function with input/output arguments, you will get an "Attempt to execute SCRIPT foo as a function" error, which
MFunctionalso gives you. Besides, it is not invalid MATLAB to call a script as a function with no arguments and no output (horrible, I know)! Try it out yourself with any script. Calling it asfoo()will simply run the script, andMFunctionshould not decide for the user what is and is not good practice.
Re 1. : Actually one of my points was that checkcode serves no purpose in MEvaluate any more. It is not needed for avoiding a hang (its original purpose) because the commands are written to a file anyway. The overhead is quite significant, so it wouldn't be bad to get rid of it and it would relax the MATLAB R2011b requirement too.
I agree it could be done away with, since the problem of engine hanging is solved by other means, but I suggest waiting a bit more, at least till we fully figure out a good roadmap for mex + evalc and change it as part of that plan (I don't think it's worth changing now and then again for mex).