HISE Logo Forum
    • Categories
    • Register
    • Login

    (Possible) Breaking change: local variables references inside nested function definitions

    Scheduled Pinned Locked Moved General Questions
    75 Posts 10 Posters 4.9k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Christoph HartC
      Christoph Hart @d.healey
      last edited by

      In C++ there is a concept where you can pass on certain variables to a local function (google lambda capture for more info), so maybe I can hack around again and frankenstein that feature into HiseScript…

      1 Reply Last reply Reply Quote 2
      • Christoph HartC
        Christoph Hart @d.healey
        last edited by

        You could use a normal function but the problem of scope would remain.

        Oh but this is wrong (at least according to my understanding of the issue). Normal functions do have a local scope that will not overwrite the global variable, but inline functions don't:

        var x = 100;
        
        inline function overwriteRootVariable()
        {
        	var x = 90;
        }
        
        function dontOverwriteRootVariable()
        {
        	var x = 1000;
        }
        
        overwriteRootVariable();
        dontOverwriteRootVariable;
        
        Console.print(x);
        
        d.healeyD 1 Reply Last reply Reply Quote 2
        • d.healeyD
          d.healey @Christoph Hart
          last edited by

          @Christoph-Hart Aha that's good to know. In that case I assume a callback function within a normal function still won't have access to the outer function's variables, correct? I like the lambda idea.

          Libre Wave - Freedom respecting instruments and effects
          My Patreon - HISE tutorials
          YouTube Channel - Public HISE tutorials

          Christoph HartC 1 Reply Last reply Reply Quote 1
          • Christoph HartC
            Christoph Hart @d.healey
            last edited by Christoph Hart

            @d-healey Ah yes, that's true, just checked.

            I've added the explicit capture syntax. It looks a bit weird coming from JS, but is inspired from the big mighty C++ again. It only works with anonymous functions so all you need to do is to put a list of all local variables and parameters between the function keyword and the parameter list

            function [variables] (parameters)
            {
               body;
            }
            

            Example usage:

            inline function testScope(p)
            {
            	local x = 900;
            	
            	Engine.showYesNoWindow("Show local value", "Click OK", function [p, x](ok)
            	{
            		Console.print(p);
            		Console.print(x);
            	});
            };
            
            function testScope2(p)
            {
            	var x = 200;
            	
            	Engine.showYesNoWindow("Show local var", "Click OK", function [p, x](ok)
            	{
            		Console.print(p);
            		Console.print(x);
            	});
            };
            
            
            testScope(10);
            testScope2(19);
            

            There are still a few limitations (eg. the captured values will be overwritten by the last call), but let me know if that helps. Edit: removed this limitation :)

            d.healeyD 1 Reply Last reply Reply Quote 3
            • d.healeyD
              d.healey @Christoph Hart
              last edited by

              @Christoph-Hart Fast work! I'll play around with this tomorrow and let you know if I hit any issues, but look like a good improvement to me.

              Libre Wave - Freedom respecting instruments and effects
              My Patreon - HISE tutorials
              YouTube Channel - Public HISE tutorials

              A 1 Reply Last reply Reply Quote 1
              • A
                andioak @d.healey
                last edited by

                @Christoph-Hart Great. Looks like the C++ examples I just read about on the Lambda / Lambda Capture pages.

                As I understand it, C++ did not have any capacity to declare a function within another function until C++ 11. At this time the Lambda function, also called an "anonymous" function (a function with no name or declaration in the global scope) was added to try and keep the global scope clean, free of unnecessary functions that are only used once. Strange that something that is so frequently used in JavaScript since forever has been added so late in the game in C++.

                The closer we are to C++ in HiseScript, the better we understand our plugins. :)

                @d-healey said in (Possible) Breaking change: local variables references inside nested function definitions:

                @Christoph-Hart Fast work! I'll play around with this tomorrow and let you know if I hit any issues, but look like a good improvement to me.

                Please post any tests you make with lambdas, I would love to have a look at that! :)

                1 Reply Last reply Reply Quote 0
                • d.healeyD
                  d.healey
                  last edited by

                  @Christoph-Hart

                  I noticed this causes a function not found error when you respond to the popup. I wonder if it's related to the new lambda stuff

                  Engine.showYesNoWindow("title", "text", function(response)
                  {
                  	
                  });
                  

                  Libre Wave - Freedom respecting instruments and effects
                  My Patreon - HISE tutorials
                  YouTube Channel - Public HISE tutorials

                  1 Reply Last reply Reply Quote 0
                  • d.healeyD
                    d.healey
                    last edited by

                    reg variable overrides the local variable

                    reg x;
                    
                    inline function test()
                    {
                    	local x = 900;
                    	
                    	Engine.showYesNoWindow("Show local value", "Click OK", function [x](ok)
                    	{
                    		Console.print(x);
                    	});
                    };
                    
                    test();
                    

                    Libre Wave - Freedom respecting instruments and effects
                    My Patreon - HISE tutorials
                    YouTube Channel - Public HISE tutorials

                    Christoph HartC 1 Reply Last reply Reply Quote 0
                    • Christoph HartC
                      Christoph Hart @d.healey
                      last edited by

                      @d-healey Both issues are fixed now.

                      d.healeyD 2 Replies Last reply Reply Quote 1
                      • d.healeyD
                        d.healey @Christoph Hart
                        last edited by d.healey

                        @Christoph-Hart Excellent, thank you! The new lambda variables and broadcaster are both excellent additions, make life so much easier :D

                        Libre Wave - Freedom respecting instruments and effects
                        My Patreon - HISE tutorials
                        YouTube Channel - Public HISE tutorials

                        1 Reply Last reply Reply Quote 0
                        • d.healeyD
                          d.healey @Christoph Hart
                          last edited by

                          @Christoph-Hart New problem. It breaks when using objects.

                          inline function test()
                          {
                          	local x = {"name":"dave"};
                          	
                          	Engine.showYesNoWindow("Show local value", "Click OK", function [x](ok)
                          	{
                          		Console.print(x.name);
                          	});
                          };
                          
                          test();
                          

                          Libre Wave - Freedom respecting instruments and effects
                          My Patreon - HISE tutorials
                          YouTube Channel - Public HISE tutorials

                          Christoph HartC 1 Reply Last reply Reply Quote 0
                          • Christoph HartC
                            Christoph Hart @d.healey
                            last edited by

                            @d-healey Ah yes, totally missed that. It's fixed now and before you ask: arrays and even functions work too now:

                            inline function test()
                            {
                            	local x = function()
                            	{
                            		Console.print("Dudel");
                            	};
                            	
                            	Engine.showYesNoWindow("Show local value", "Click OK", function [x](ok)
                            	{
                            		x();
                            	});
                            };
                            
                            d.healeyD 1 Reply Last reply Reply Quote 1
                            • d.healeyD
                              d.healey @Christoph Hart
                              last edited by

                              @Christoph-Hart Thanks for the fast work!

                              Libre Wave - Freedom respecting instruments and effects
                              My Patreon - HISE tutorials
                              YouTube Channel - Public HISE tutorials

                              Christoph HartC 1 Reply Last reply Reply Quote 0
                              • Christoph HartC
                                Christoph Hart @d.healey
                                last edited by

                                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.

                                d.healeyD ustkU 2 Replies Last reply Reply Quote 2
                                • d.healeyD
                                  d.healey @Christoph Hart
                                  last edited by

                                  @Christoph-Hart That's nice, I've been in the same habit of declaring local versions

                                  Libre Wave - Freedom respecting instruments and effects
                                  My Patreon - HISE tutorials
                                  YouTube Channel - Public HISE tutorials

                                  1 Reply Last reply Reply Quote 0
                                  • ustkU
                                    ustk @Christoph Hart
                                    last edited by

                                    @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?

                                    Can't help pressing F5 in the forum...

                                    d.healeyD Christoph HartC 2 Replies Last reply Reply Quote 0
                                    • d.healeyD
                                      d.healey @ustk
                                      last edited by d.healey

                                      @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); 
                                      

                                      Libre Wave - Freedom respecting instruments and effects
                                      My Patreon - HISE tutorials
                                      YouTube Channel - Public HISE tutorials

                                      ustkU 1 Reply Last reply Reply Quote 1
                                      • ustkU
                                        ustk @d.healey
                                        last edited by

                                        @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... :)

                                        Can't help pressing F5 in the forum...

                                        d.healeyD 1 Reply Last reply Reply Quote 0
                                        • d.healeyD
                                          d.healey @ustk
                                          last edited by

                                          @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);
                                              });
                                          }

                                          Libre Wave - Freedom respecting instruments and effects
                                          My Patreon - HISE tutorials
                                          YouTube Channel - Public HISE tutorials

                                          ustkU 1 Reply Last reply Reply Quote 1
                                          • ustkU
                                            ustk @d.healey
                                            last edited by

                                            @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...

                                            Can't help pressing F5 in the forum...

                                            1 Reply Last reply Reply Quote 1
                                            • First post
                                              Last post

                                            47

                                            Online

                                            1.7k

                                            Users

                                            11.7k

                                            Topics

                                            101.9k

                                            Posts