cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
mat-ski
Level III

Values logged in Print not matching values in script executions

Hi, I am trying to understand some behavior in a function that uses Default Local scope. Namely, the values that appear in my logs when using Print statements do not match the values that my code is executed with. For example in this script:

 

 

Names Default To Here( 1 );

EXAMPLE_VALUE = "100";

showExampleModal = Function( {},
	{Default Local},
	
	Print("Here:EXAMPLE_VALUE: " || Char(Here:EXAMPLE_VALUE));
	Print("EXAMPLE_VALUE: " || Char(EXAMPLE_VALUE));

	modal = New Window( "Example Modal",
		<<Modal,
		Border Box( Left( 2 ), Right( 2 ),
			V List Box(
				H Center Box( Text Box( "Here:EXAMPLE_VALUE: " || Char(Here:EXAMPLE_VALUE) ) ),
				H Center Box( Text Box( "EXAMPLE_VALUE: " || Char(EXAMPLE_VALUE) ) ),
			), 

		),
	);
);

In my logs I see:

 

 

"Here:EXAMPLE_VALUE: 100"
"EXAMPLE_VALUE: 100"

However in my modal I see:

 

Here:EXAMPLE_VALUE: 100
EXAMPLE_VALUE: .
5 REPLIES 5

Re: Values logged in Print not matching values in script executions

@mat-ski, when declaring the Default Local namespace in a function, all unscoped variables are local to that function (see here). EXAMPLE_VALUE is declared outside the function, so the modal displays the correct value only when the variable reference is scoped to Here. Declaring EXAMPLE_VALUE inside the function will make it local and available to the function, but it then wouldn't exist in the Here namespace to be accessible to the rest of the script. Here's a helpful page on namespace referencing.

Ross Metusalem
JMP Academic Ambassador
mat-ski
Level III

Re: Values logged in Print not matching values in script executions

According to the documentation that you linked on namespace referencing it states that for user defined functions with Default Local "reference rule is unchanged" and the reference rule for unqualified references states: 

If the Names Default To Here mode is on, JMP looks for the variable in these locations:

  • Local namespace
  • Here namespace
  • current data table

 

So it seems like the documentation is stating that it should still fallback to Here in case there is no local variable with the name in question in the Local namespace.

Separately, it seems problematic to me that the behavior of the Print statement does not match the behavior of the context from which the Print is being made.

vince_faller
Super User (Alumni)

Re: Values logged in Print not matching values in script executions

When dealing with scopes in JSL. I pretty much always say just be explicit if you're moving about between them.  I've spent DAYS trying to debug a single scoping issue.  

Vince Faller - Predictum
mat-ski
Level III

Re: Values logged in Print not matching values in script executions

Based on some further debugging I believe that I understand what is happening.

 

Updated code:

 

Names Default To Here( 1 );

EXAMPLE_VALUE = "100";

showExampleModal = Function( {},
	{Default Local}, 

	// (1)
	hereStr = "Here:EXAMPLE_VALUE: " || Char( Here:EXAMPLE_VALUE );
	unqualifiedStr = "EXAMPLE_VALUE: " || Char( EXAMPLE_VALUE );

  // (2)
	hereStrTextBox = Text Box( "Here:EXAMPLE_VALUE: " || Char( Here:EXAMPLE_VALUE ) );
	unqualifiedStrTextBox = Text Box( "EXAMPLE_VALUE: " || Char( EXAMPLE_VALUE ) );
	
  // (3)
	Print( hereStr );
	Print( unqualifiedStr );

	modal = New Window( "Example Modal",
		<<Modal,
		Border Box( Left( 2 ), Right( 2 ),
			V List Box(
        // (4)
				H Center Box( Text Box( hereStr ) ), // Defined
				H Center Box( Text Box( unqualifiedStr ) ), // Defined
				H Center Box( Text Box( "Here:EXAMPLE_VALUE: " || Char( Here:EXAMPLE_VALUE ) ) ), // Defined
				H Center Box( Text Box( "EXAMPLE_VALUE: " || Char( EXAMPLE_VALUE ) ) ), // Not Defined
				H Center Box( hereStrTextBox ), // Defined
				H Center Box( unqualifiedStrTextBox ), // Defined

			), 

		), 

		H List Box(
			Spacer Box(),
			Button Box( "OK", 
        // (5)
				Print( "On Click" );
				Print( "Here:EXAMPLE_VALUE: " || Char( Here:EXAMPLE_VALUE ) ); // Defined
				Print( "EXAMPLE_VALUE: " || Char( EXAMPLE_VALUE ) ); // Not defined
			)
		), 

	);
);

 

 

At (1) the assigments are evaluated right away and so when they are passed later to the Print/Text Box they are defined as I expect. This is confirmed by the output of (3) and the first to Text Boxes of (4).

 

At (2) the Text Boxes are again evaluated at the time the line is executed, and so when they are displayed, they are again defined using the values that I expect. This is confirmed by the last two Text Boxes of (4)

 

The middle two Text Boxes of (4) are not in fact evaluated immediately, but passed to the Center Box (or maybe the List Box?) as expressions, and evaluated later/in another scope (I guess the scope of either the List Box or the Center Box). As a result the value of EXAMPLE_VALUE is not what I expected, instead it is the value of EXAMPLE_VALUE in whatever other scope it is being evaluated in (namely it is undefined).

 

This seems to be confirmed by (5), which also passes the Prints as expressions to be evaluated later / in another scope.

 

Is this accurate?

vince_faller
Super User (Alumni)

Re: Values logged in Print not matching values in script executions

boxes have their own scope. currently you're doing a modal window but imagine if you weren't and the function closed and all the local variables are now gone.  For stuff like that I usually do something like.  

 

Names default to here(1);
test = function({something_passed, kwargs={}},
    {DEFAULT LOCAL},
    // Kwargs
    kwarg = Try(kwargs["kwarg"], "default"); // kwarg description
    something_not_passed = 14;
	
    nw = new window("Scope",
		window:something_not_passed = something_not_passed; // just assignthe variable to the display
		window:something_passed = something_passed; 
		window:something_direct = 42;

		buttonbox("OK", 
			<<Set Function(
				Function({self}, {DEFAULT LOCAL},
					show(window:something_not_passed, window:something_passed, window:something_direct);
				)
			)
		)
    );

    return(nw);

);
nw = test(28);
//your variables are gone now, but when you click okay they're still in the box
Vince Faller - Predictum