cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
jthi
Super User

Issue with Function() default local and Each functions

I have already contacted JMP support about this and this has been marked as a bug. Here is simple "real" case where this bug could be seen

Names Default To Here(1);

dt = Open("$SAMPLE_DATA/Big Class.jmp");

f2 = function({}, {Default Local},
	Summarize(dt, uniq_values = by(:sex));
	all_sex_values = dt[0, "sex"];
	
	//dt = dt; // don't remove, it just works
	For Each({uniq_value}, uniq_values, 
		cur_sex_rows = Loc(all_sex_values, uniq_value);
		
		show(all_sex_values, uniq_value, cur_sex_rows, dt); // dt goes poof inside for each
		
		vals = Transform Each({val}, dt[cur_sex_rows, "age"], Output("List"), Char(val));
		show(vals);
	);
);

f2();
all_sex_values = {"F", "F", "F", "F", "F", "M", "M", "M", "F", "F", "F", "M", "M", "M", "M", "F", "F", "F", "F", "F", "M", "M", "M", "M", "M", "M", "M", "F", "F", "M", "M", "M", "M", "M", "F", "F", "M", "F", "M", "M"};
uniq_value = "F";
cur_sex_rows = [1, 2, 3, 4, 5, 9, 10, 11, 16, 17, 18, 19, 20, 28, 29, 35, 36, 38];
dt = ␀;
argument value is invalid in access or evaluation of 'Transform Each' , Bad Argument(dt[cur_sex_rows, "age"]), Transform Each/*###*/({val}, dt[cur_sex_rows, "age"], Output("List"), Char(val))

at line 15 in mypath

 

I was working on a script in which I have to encrypt small part of it, so I was wrapping most of the code I have to encrypt inside single function to minimize the chance of my variables leaking (I'm lazy like that in this case). While writing this excessively large single function, I noticed that I was getting "argument value is invalid" errors inside my For Each loop even though all the variables were defined. I did some testing and figured out that for each (and other each loops) weren't handling Default Local correctly (or it seemed like that). Below are two examples

 

Names Default To Here(1);

thing = Expr(
	Summarize(dt, uniq_values = by(:sex));

	all_sex_values = dt[0, "sex"];

	For Each({uniq_value}, uniq_values,
		cur_sex_rows = Loc(all_sex_values, uniq_value);
		
		vals = Transform Each({val}, dt[cur_sex_rows, "age"], Output("List"), Char(val));
		show(vals);
	);
);

f1 = function({}, {Default Local},
	thing;
);


f2 = function({}, {Default Local},
	Summarize(dt, uniq_values = by(:sex));
	all_sex_values = dt[0, "sex"];
	
	//dt = dt; // don't remove, it just works
	For Each({uniq_value}, uniq_values, 
		cur_sex_rows = Loc(all_sex_values, uniq_value);
		
		show(all_sex_values, uniq_value, cur_sex_rows, dt); // dt goes poof inside for each
		
		vals = Transform Each({val}, dt[cur_sex_rows, "age"], Output("List"), Char(val));
		show(vals);
	);
);


f3 = function({}, {all_sex_values, cur_sex_rows},
	Summarize(dt, uniq_values = by(:sex));

	all_sex_values = dt[0, "sex"];

	For Each({uniq_value}, uniq_values,
		cur_sex_rows = Loc(all_sex_values, uniq_value);
		
		vals = Transform Each({val}, dt[cur_sex_rows, "age"], Output("List"), Char(val));
		show(vals);
	);
);

dt = Open("$SAMPLE_DATA/Big Class.jmp");

Print("Thing Expr");
Try(thing, show(exception_msg));

Print("\!NThing Expr inside Function");
Try(f1(), show(exception_msg));

Print("\!NThing Function Only default local");
Try(f2(), show(exception_msg));

Print("\!NThing Function Only local list");
Try(f3(), show(exception_msg));

Close(dt, no save);

 

 

Names Default To Here(1);

my_val = 1;
my_func1  = function({}, {Default Local},
	show(my_val);
);

my_func2 = function({}, {Default Local},
	my_func1();
);

my_func22 = function({}, {Default Local},
	my_func2();
);

my_func222 = function({}, {Default Local},
	my_func = function({}, {Default Local},
		show(my_val);
	);
	my_func();
);

my_func3 = function({}, {Default Local},
	my_func1(); // even this goes null
	For Each({z}, {"b","c"}, show(my_val));
);

my_func1();
my_func2();
my_func22();
my_func222();
my_func3();

 

I'm (secretly) proud that I can leave this (for now) to my production script as I haven't had those yet (but I was helpful and added at least the case number there for future me).

 

dt = dt; // don't remove, it just works (JMP Support Case 00075244)

 

 

I did get response regarding this from JMP Support personnel who had contacted JMP developer:

This looks like a bad interaction between Function Default Local and the Each functions. Here is a failing test:

ut test("EachLoop", "Default Local in Body Expr", Expr( 
      defloc = 5;
      f = Function({}, {Default Local}, Transform Each({x}, 1::5, x*defloc));
      ut assert that(Expr(f), [5 10 15 20 25])
));

Since defloc is an unqualified name that appears in the function body, it is marked as a default local. This means JMP automatically creates a local if we write to the symbol. However, when reading from it like in the above script, it should skip the local frame and go up to the next frame and the next until Here and Global. This is the expected behavior and the one you get if you read defloc outside of the Each function. But, if you read inside the Each function, this process breaks for some reason. Possibly because the Each function introduces another frame itself since it can also have locals.

 

The dt = dt is a workaround, as is avoiding Default Local entirely.

-Jarmo
0 REPLIES 0