Returning an expression from a function differs when namespace is used.
Jul 16, 2023 09:34 PM(896 views)
Hello everyone,
I ran into some strange behavior when returning an expression from a function, and could not find any discussions about this already. I am in the habit of using namespaces for pretty much everything. I found that when a function returns an expression, the expression is not evaluated for an unscoped function, but is evaluated if a namespace is used. Does anyone know what's going on here?
This script should show that the variable x is not incremented when the first function is called, but is incremented on the second function call. I am using JMP v17.0.0, so this has possibly been fixed, but I am not able to update right now.
The two issues are probably related. Items specified inside the namespace function call are not constructed until they are used (they are kept as expressions.) That means f1 is not compiled when the namespace is created, but f1 is compiled each time it is used, thus slower. So there is probably some specialized C++ code to recompile f1 and then evaluate it. My guess is that same code is triggered for f2, but doesn't recompile because f2 is already compiled, and then incorrectly evaluates the result of f2, which most of the time is a primitive value and no one notices. The expr() value that is returned does evaluate into a simpler value, causing you to notice.
The two issues are probably related. Items specified inside the namespace function call are not constructed until they are used (they are kept as expressions.) That means f1 is not compiled when the namespace is created, but f1 is compiled each time it is used, thus slower. So there is probably some specialized C++ code to recompile f1 and then evaluate it. My guess is that same code is triggered for f2, but doesn't recompile because f2 is already compiled, and then incorrectly evaluates the result of f2, which most of the time is a primitive value and no one notices. The expr() value that is returned does evaluate into a simpler value, causing you to notice.