Keeping local scopesI am trying to create a framework for visualizing data -- this leads me to create a template with several buttons that can act on the data that was passed into the template.
However, I am running into an issue with scoping where creating a button with a function to call -- calling this function happens in a different scope than where I would have thought based on the location of the button in the code.
I've provided a minimum working example below -- I would expect that when each button is clicked the resulting function call would happen within the original defining scope.
Is there a way to accomplish this?
Thanks!
Names Default To Here ( 1 );
insert_text = Function({tb, msg}, {Default Local},
tb << Add ( "Added", msg )
);
tab_function = Function({msg}, {Default Local},
Tab Box (
"one",
tb1 = Tab Box();
V List Box(
b = Button Box ( "Add", insert_text(tb1, msg))
);,
"two",
tb2 = Tab Box();
V List Box(
b = Button Box ( "Add", insert_text(tb2, msg))
);,
"three",
tb3 = Tab Box();
V List Box(
b = Button Box ( "Add", insert_text(tb3, msg))
);
)
);
New Window(
"Test Window",
tab_function("hi");
)Re: Keeping local scopesPlease notice in the previous script that the insert_text function is referencing the tab, tb, and the msg, msg. The <
First of all, I wouldn't use a function to create the tabs. I modified the script to use tab page boxes and I create a variable or them. Note since you are using a function, if you specify all variables but msg are local, the variable references will be unknown once tab_function runs. I commented our {Default Local}; uncomment it to see the errors.
I modified the insert_text function to demonstrate the script knows which tab button was pressed and you can get info about that tab page. Test each tab's button.
Names Default To Here ( 1 );
insert_text = Function({tb, msg}, {Default Local},
tb << select;
str = tb << get title;
str = "I am on tab " || str || " with message " || msg;
Caption(str); wait(2); Caption(remove);
tb << Append (TextBox( msg ));
tb << deselect;
);
tab_function = Function({msglist}, //{Default Local},
Eval(Eval Expr(
mytb= Tab Box (
tb1=Tab Page Box( "one",
V List Box(
b = Button Box ( "Add", insert_text(tb1, expr(msglist[1])) )
)
),
tb2 =Tab Page Box( "two",
V List Box(
b = Button Box ( "Add", insert_text(tb2, expr(msglist[2])) )
)
),
tb3=Tab Page Box("three",
V List Box(
b = Button Box ( "Add", insert_text(tb3, expr(msglist[3])) )
)
)
)
) )
);
New Window(
"Test Window",
tab_function({"hi","stay awhile", "goodbye"} );
);
//this command with throw an error with {Default Local}...once the tab_function runs tb1, etc are unknown
show( mytb << class name, tb2<<GET title=""></GET>Re: Keeping local scopesThanks for helping! However, this is not precisely what I am looking for -- I need to be able to reference variables local to "tab_function" from within the "insert_text" function by passing a reference of said variable into the "insert_text" function.
Re: Keeping local scopesI am not sure what you are trying to do. But this is my best guess. I modified a few things.
Names Default To Here ( 1 );
insert_text = Function({tb, msg}, {Default Local},
tb << select;
Caption(msg); wait(2); Caption(remove);
tb << Sib Append (TextBox( msg ));
tb << deselect;
);
tab_function = Function({msg}, {Default Local},
Eval(Eval Expr(
Tab Box (
"one",
V List Box(
b = Button Box ( "Add",
<<SET function=""></SET>