Subscribe Bookmark RSS Feed

Pass Empty Variables to Function

msharp

Super User

Joined:

Jul 28, 2015

The below code doesn't work, b/c JSL evaluates emptyVar when you try to pass it to the function instead of evaluating it on run time w/in the function.

 

isEmptyTest = Function({var},
	return(isempty(var));
);
isEmptyTest(emptyVar);

 

It would be awesome if this was changed.

 

2 REPLIES
Craige_Hales

Staff

Joined:

Mar 21, 2013

A symbol table keeps track of all the variables that have ever been set to a value. If a variable has never been set, it is uninitialized, and using it is probably an error.

The builtin isEmpty() function makes special cases out of several unusual situations.

Case 1: variable never used before

 

Is Empty(neverSeenBefore) = 1;

print(neverSeenBefore)
/*:
Name Unresolved: neverSeenBefore in access or evaluation of 'neverSeenBefore' , neverSeenBefore/*###*/

 

Name Unresolved means JMP looked in the symbol table and could not locate the name.

Case 2 (normal case): variable set to the value returned by the builtin empty() function, or another function that returned empty()

 

ev=empty(); show(isempty(ev),ev);
/*:
Is Empty(ev) = 1;
ev = Empty();

 

If you expect to test for empty, you should use the empty() function to initialize your variables.

Case 3: variable cleared by the builtin clear globals() function

 

ev2=42; clear globals(ev2); show(isempty(ev2),ev2);
Is Empty(ev2) = 1;
ev2 = ev2;

Using clear globals() isn't a good idea in production code, but can be helpful while developing code. It does NOT remove the symbol from the symbol table but does remove its value, not even leaving empty() for the value. It may act like empty() or missing, depending how it is used.

 

JSL passes arguments to user written functions by value, making a copy for the function to use. A future version of JMP may allow passing arguments by reference. We'll see what happens with passing uninitialized variables by reference at that point.

 

 

Craige
msharp

Super User

Joined:

Jul 28, 2015

Yeah, I was trying to use it for Case 1.  I was building a tool that took advantage a user config file.  Since variables are loaded from the file that's user defined, they may or may not exist and may or may not be set to true/false.  I was hoping to just build a wrapper function that would test this until I realized a could do an "or" statement since it exits once it hits a truthy.

 

if(isempty(var) | !var,
  //run code)
);

This doesn't error out b/c if the variable hasn't been declared yet it will signal a Truthy and move on to run the code in the if statement w/o checking the other dependencies.  If the variable has been defined then I can check if it's true or not.