cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • JMP 19 is here! See the new features at jmp.com/new.
  • Register to attend Discovery Summit 2025 Online: Early Users Edition, Sept. 24-25.
Choose Language Hide Translation Bar
bculver
Level II

Returning an expression from a function differs when namespace is used.

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.

 

NewNamespace("varNamespace");
varNamespace:x = 0;
myFunc = Function({}, {DEFAULTLOCAL}, returnVal = Expr(varNamespace:x++); NameExpr(returnVal); ); myFunc(); Show(varNamespace:x); NewNamespace("functionTest"); functionTest:myFunc = Function({}, {DEFAULTLOCAL}, returnVal = Expr(varNamespace:x++); NameExpr(returnVal); ); functionTest:myFunc(); show(varNamespace:x);
10 REPLIES 10
hogi
Level XII

Re: Returning an expression from a function differs when namespace is used.

Names Default to Here(1);

x= Expr("Hello");

funcNS = New Namespace("funcNS");
funcNS:f = Function( {}, {Default Local},
	x = Expr(1+2);
	Substitute(Expr(Expr(ex)), Expr(ex), (Name Expr(x)))
);
	
Show( funcNS:f() , Type( funcNS:f() ) ); // expr, expr 
  • go give the function a chance to look up x inside the function:   Name Expr() instead of Expr().
  • to prevent the namespace from evaluating the return value "twice", the return value has to be protected with Expr()
  • this makes the life difficult for the Name Expr() 
    Expr(Name Expr()) will stop Name Expr() from doing it's job
  • In 99% of such cases, one can use Eval Expr().
    But here it's difficult, because Expr() is used in JMP for 2 purposes:
    "it is here()" inside of Eval Expr()
    - I protect you()  - as a solution to the namespace issue
    unfortunately, Eval Expr() will be happy with the first Expr() and evaluate it.
    so, Eval Expr(Expr(Expr(Name Expr(x)))) will not work in the spirit of 
    Eval Expr ( I protect you ( it is here(Name Expr(x))))
    instead it  interpretes it as
    Eval Expr( it is here(I protect you(Name Expr(x))))
    and therefore it will return 
    Expr(Name Expr(x))
    ... and then JMP will search for x outside of the scope of the function 

  • The ultimate solution to all Expression issues is Substitute()
    Re: Expression Handling in JMP: Tipps and Trapdoors 
  • is there an easier solution than 
    Substitute(Expr(Expr(ex)), Expr(ex), (Name Expr(x)))

Recommended Articles