MATLink icon indicating copy to clipboard operation
MATLink copied to clipboard

Rethink how and when checkcode() is used

Open szhorvat opened this issue 12 years ago • 5 comments

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:

  1. remove checkcode from MEvaluate to reduce overhead
  2. add checkcode to MScript and MFunction: it should be run when a script is created
  3. If possible, make MFunction verify that the file it creates is a true function and not just a script (is there a MATLAB function for this)?

szhorvat avatar Apr 09 '13 18:04 szhorvat

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

szhorvat avatar Apr 10 '13 20:04 szhorvat

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.

szhorvat avatar Apr 11 '13 02:04 szhorvat

Hmm... I don't think some of this is necessary.

  1. There is the undocumented MEvaluate[..., "NoCheck"] for this, to reduce overhead. Besides, repeated runs of the same input should be in an MScript, which when called with MEvaluate, will not check for errors.
  2. 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).
  3. 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 MFunction also 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 as foo() will simply run the script, and MFunction should not decide for the user what is and is not good practice.

rsmenon avatar Apr 11 '13 21:04 rsmenon

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.

szhorvat avatar Apr 21 '13 14:04 szhorvat

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).

rsmenon avatar Apr 21 '13 20:04 rsmenon