(Possible) Breaking change: local variables references inside nested function definitions
-
@Christoph-Hart Thanks for the fast work!
-
Now that I'm currently in "fixing annoying things in the HiseScript compiler" mode, I also fixed leaking of iterator variables into the global namespace:
inline function inner() { for(i = 0; i < 10; i++) Console.print(i); } function outer() { for(i = 0; i < 5; i++) inner(); } Console.print(i);
This will have rather unpredictable results because the
i
in the inline function is actually using the same scope as the one in the outer function, which will leak also to the global scope. In order to fix this, you previously needed to declare local / var statements before the loop like this:inline function inner() { local i = 0; for(i = 0; i < 10; i++) Console.print(i); } function outer() { var i = 0; for(i = 0; i < 5; i++) inner(); } Console.print(i);
Which I've grown accustomed to do whenever I was using these kind of generic loop iterator names that might be called in a nested fashion. However that shouldn't be the responsibility of the user but a language feature, so I've added some code to the compiler to actual inject these local / var statements before the for loop so it makes sure it uses the most narrow scope possible when using iterator variables.
-
@Christoph-Hart That's nice, I've been in the same habit of declaring local versions
-
@Christoph-Hart
Can't capture anonymous expression
Might be just normal, I'm not familiar enough with lambdas...
for (t in things) { bgPnl.setPaintRoutine(function [t](g){}); // Can't capture anonymous expressions var thing = t; bgPnl.setPaintRoutine(function [thing](g){}); // ok }
Just for my personal knowledge, why is it anonymous?
-
@ustk
t
is anonymous, as in you didn't declare it. HISE just magicked it out of a nowhere when you needed it for your loop. What about this approach?function myPaintRoutine(g) { var things = this.data.things; } bgPnl.data.things = things; bgPnl.setPaintRoutine(myPaintRoutine);
-
@d-healey Thanks for clarifying
Yeah, I can find a way to o what I need, it's just that for once I could find a purpose for using lambdas, disillusion... :) -
@ustk Here's the kind of thing I use lambdas for
inline function promptForSomething(name) { Engine.showYesNoWindow("Would you like to view " + name + "?", function [name](response) { if (response) show(name); }); }
-
@d-healey In fact I'm mistaken as I used it in another project the same way you do:
inline function ondriverCbControl(component, value) { Engine.showYesNoWindow(Titles.ChangeDriver, Messages.WarningDeleteCaptures, function[value](result) { if (result) { Settings.setAudioDeviceType(drivers[value-1]); } }); }; driverCb.setControlCallback(ondriverCbControl);
But it's the first time I try in a for loop though...
-
@ustk David is right, the iterator variable is not part of the function scope that can be passed to the lambda. Not sure if this is an oversight or if there is a good reason, but I always create variables like in the second line too. Think of it as picking up a nice bouquet in order to surprise your lambda friend.
-
@Christoph-Hart Love the analogy :)
-
@ustk Yeah, it was definitely worth googling what that phrase sounds like in English...
-
@Christoph-Hart bouquet is a french word :)
-
-
Some of us were already quite familiar this one.
-
@d-healey Oh boy I miss so much that brit humour
-
@d-healey do you think its possible having this in my project is some how causing my note release issue?
-
@Adam_G No because that issue is happening in a project without any scripting
-