Limit scope of Scilab variables - function

I use the code
clc;
clear;
getd();
a=1;
b=myFunction();
, where myFunction is defined by
function b=myFunction()
b=a+1;
endfunction
. For some strange reason, this works just fine in Scilab. I believe myFunction simply inherits a from the main function. This is in contrast with for instance Matlab, where a needs to be an input argument of myFunction in order to use it.
I want functions in Scilab to only work with local variables and variables given as input, like in Matlab. So that in this case a is not inherited from the main function.
How can I achieve this?

This works fine in Scilab because in case a function uses an undefined variable the interpreter search it in the calling scope. Although it seems strange, this is how it works and I think this behavior cannot be changed without modifying the source code as #NormalHuman said.
In your example your code is working because you have the "a" variable is defined in the calling scope, but if you execute the function in another situation it could fail. In my opinion a function defined in this way has a defect in its code.
I am using a lot Scilab and I don't see this as a real problem, but I agree with you that this behavior is quite strange and it should not exist.
However, if you are worried of developing incorrectly a function that works well while you are writing the code due to a variable defined in calling scope, the solution for that is to create some unit tests and execute them in a clean interpreter.

You can avoid this by choosing distinct names for variables you wish to be local.
Like putting a myFunction_ in front of all the local variable names. In your example, you would rename a into myFunction_a.
Unfortunately, I don't know if it would be possible to write a script that would do the work for you in case you have large matlab scripts.

Related

SolidJS: "computations created outside a `createRoot` or `render` will never be disposed" messages in the console log

When working on a SolidJS project you might start seeing the following warning message in your JS console:
computations created outside a `createRoot` or `render` will never be disposed
There are some information available on this in SolidJS' Github repository issues. But after reading them I was still not quite sure what this was all about and whether my code was really doing something wrong.
I managed to track down where it came from and find a fix for it based on the documentation. So I'm providing the explanation and the solution for those Googling this warning message.
In essence this is a warning about a possibility of a memory leak due to a reactive computation being created without the proper context which would dispose of it when no longer needed.
A proper context is created a couple of different ways. Here are the ones I know about:
By using the render function.
By using the createRoot function. Under the hood render uses this.
By using the createContext function.
The first is by far the most common way, because each app has at least one render function call to get the whole show started.
So what makes the code go "out of context"?
Probably the most common way is via async calls. The context creation with its dependency tree happens only when the synchronous portion of the code finishes running. This includes all the export default function in your modules and the main app function.
But code that runs at a later time because of a setTimeout or by being in an async function will be outside of this context and any reactive computations created will not be tracked and might stick around without being garbage collected.
An example
Let's say you have a data input screen and have a Save button on it that makes an API call to your server to save the data. And you want to provide a feedback to the user whether the operation succeeded or not, with a nice HTML formatted message.
[msg,setMsg] = createSignal(<></>)
async function saveForm(){
...
setMsg(<p>Saving your data.<i>Please stand by...</i></p>)
const result=await callApi('updateUser',formData)
if(result.ok){
setMsg(<p>Your changes were <b>successfully</b> saved!</p> )
} else {
setMsg(<p>There was a problem saving your data! <br>Error: </p><pre>{result.error}</pre> )
}
}
...
<div>
...
<button onClick={saveForm} >Save</button>
{msg()}
</div>
This will produce the above mentioned warning when the API call returns an error, but not the other times. Why?
The reason for this is that SolidJS considers the code inserts inside JSX to be reactive, ie: need to be watched and re-evaluated. So inserting the error message from the API call creates a reactive computation.
The solution
I found the solution at the very end of the SolidJS doc. It's a special JSX modifier: /*#once*/
It can be used at the beginning of a curly brace expression and it tells the SolidJS compiler to explicitly not to make this a reactive expression. In other words: it will evaluated once and only once when the DOM nodes are created from the JSX.
In the above example here's how to use it:
setMsg(<p>There was a problem saving your data! <br>Error: </p><pre>{/*#once*/ result.error}</pre> )
After this there will be no more warning messages :)
In my case, I had an input and when that input changed I re-created an SVG drawing. Because the SVG creation was an expensive operation, I added a debounce in the createEffect function which ran when the input changed. debounce is a technique to defer the processing until the input stops changing for at least X amount of time. It involved running the SVG generation code inside the setTimeout function, thus being outside of the main context. Using the /*#once*/ modifier everywhere where I inserted an expression in the generated JSX has fixed the problem.

F# Guid.Parse TypeInitializationException

I'm working on a WebApi project written in F#. Here a snippet:
module MyModule
open System
let MyGuid = Guid.Parse "934F0B12-D00A-491D-862D-EE745EF3C560"
let myFunction list =
list.Get(MyGuid) // --> here MyGuid has the TypeInitializationException before list.Get is called
By debugging I can see that the MyGuid actually has an error
Changing the code followings, it works:
module MyModule
open System
let MyGuid () = Guid.Parse "934F0B12-D00A-491D-862D-EE745EF3C560"
let myFunction list =
list.Get(MyGuid())
I actually know the MyGuid of the first example is a variable and the second one a function definition, but why does the first rise the exception? I my code MyGuid is used some times. so in the first example I'd have only one instance, in the second a new instance every time MyGuid is called...
I'm not 100% sure that this is the problem here, but I've seen similar behaviour when using unit test runners sometimes. My guess is that the error happens because the top-level MyGuid variable is not initialized correctly and has the default zero value (and as a result, the lookup fails).
The way global variables are initialized in F# is tricky - if you compile code as executable, this can happen from the Main method. But if you compile code as a library, the compiler inserts an initialization checks into static constructors of the types in your library (to make sure everything is initialized before you access anything).
I think this can break if you compile your code as an executable, but then load it as a library - the entry-point is not called and so the variables are not initialized. I'm not sure how exactly WebApi loads libraries, but this could be a problem - especially if you compile the F# code as an executable.
Your workaround of turning the global variable into a function fixes this, because the function is compiled as a method and so you avoid referring to an uninitialized value. Sadly, I don't think there is a better workaround for this.

Google Apps Script order of execution: why do functions get invoked merely by being defined?

In Javascript, the following code will execute only once, but in Google Apps Script it executes twice (obviously the body and script tags would be omitted):
<body>
<script>
hi();
function hi() {
alert('hi')
}
</script>
</body>
In other words, in GoogleApps script merely defining a function invokes it. The following in Code.gs executes with undefined arguments passed to it.
function createQuery(keywords, dateRange) {
}
How can I define functions without them being called? Pointers to the docs would be helpful. I have scoured them without success.
In GAS, you save and test functions using the toolbar at the top. By virtue of telling the function to run using the toolbar, you are calling the function. Any triggers that you have will also call it to run without 'calling' inside the code.
You can more minutely determine how and when a function runs by calling it later in the code. This can be done in nested functions or inside of other functions. Regardless of where you are defining the function, eventually you will have to either create a trigger or use the toolbar to run the function.
I'm not sure how you are experiencing a double call seeing as your reference code is not very in-depth. Keep in mind that to execute the script at all, you would have had to either set a trigger or run it yourself in which you are adding in an execution. If you execute code that tells itself to execute that same code, you would incur an infinite loop.
Users will be able to more accurately understand your question and issue and thus answer them should you provide more data and references. You may also want to read the references and guides on the basics of GAS and its use of Javascript.

Application happend unavailable parameter in excute runtime

After Install shield, I create a application complete and I excute it,
But something like follow picture. I cant debug it, and I cant fint the error what happend.
Someone can tell me, what method to solve it...
1) Like function parameter no use?
Well, I find a rule.
When run the application, the OnCommand Function is early than OnInitDialog function,
the OnCommand will handle static, buttom, listcontrol, color etc.
but My initial variable is in OninitDialog,
So it will append this result.
The solution is declare a variable to avoid into the function, when complete the OninitDialog.

Unable to use a matlab function

I wanted to move from one point to other on a spherical earth, and when I looked up, I found there is a function named reckon in matlab that does exactly what I need. But when I call it, it says Undefined function 'reckon' for input arguments of type 'double. Which means that maybe the function is not in my library. So I found the m-file from the internet and tried, but then it leads to same error with a different function, which I found the function reckon depends on. So I included that in my folder too, and then again there is a new function missing... and so on.
I have Matlab R2011b.
These functions seem to be in-built matlab functions as they show up in help, but as I'm new to matlab, maybe I'm wrong. What can be done?
As i just wanted to use the functions of the Mapping Toolbox,after some looking up, i found them all in a single package on a website. If any body else wants them too, and don't have the mapping toolbox, you can get all the functions here.
http://mooring.ucsd.edu/software/matlab/doc/toolbox/geo/index.html