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.
    • d.healeyD
      d.healey @ustk
      last edited by

      @ustk hr comes from the file browse call but the scope doesn't extend to the second browse call so it needs to be stored in a temporary variable

      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
        last edited by ustk

        Just to say that I updated my custom branch, and all my var changed to local in the current project that is dependent on this very custom branch.
        It's not a big deal to change back everything though as I remember this fix. But in the case it can be fixed it could help other people not to have this surprise, without knowing where it comes from :)

        orangeO 1 Reply Last reply Reply Quote 1
        • ustkU
          ustk
          last edited by ustk

          This post is deleted!
          1 Reply Last reply Reply Quote 0
          • orangeO
            orange @ustk
            last edited by orange

            @ustk Same here. I've used tons of local variables in all of my projects. Now this shit comes...

            alt text

            develop Branch / XCode 13.1
            macOS Monterey / M1 Max

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

              @orange This problem was always there, it's just now you get a warning. It only applies to local variables in certain circumstances.

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

              orangeO 1 Reply Last reply Reply Quote 0
              • orangeO
                orange @d.healey
                last edited by

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

                @orange This problem was always there

                My previous projects can't be opened with the latest develop branch.
                But if I modify the local variables as var it works. So this issue is a new one.

                develop Branch / XCode 13.1
                macOS Monterey / M1 Max

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

                  @orange

                  But if I modify the local variables as var it works. So this issue is a new one.

                  No it's an old issue, but now there is a new error so it might cause problems with opening old projects.

                  You don't need to change all locals to var - in fact that is a really bad idea. You only need to change the ones that are in nested functions. And I recommend not changing them to var because that pollutes the scriptwide namespace, I think it's better to create a reg object at the top of each namespace that can be used to hold the values - should I make a video about this?

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

                  orangeO 1 Reply Last reply Reply Quote 3
                  • orangeO
                    orange @d.healey
                    last edited by

                    @d-healey Ok I will try with reg thanks.

                    So only modifying the places that the Hise gives the warnings is enough? Or even Hise doesn't give warnings, I should modify all nested functions?

                    develop Branch / XCode 13.1
                    macOS Monterey / M1 Max

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

                      @orange

                      Ok I will try with reg thanks.

                      Just 1 reg at the top of the namespace.

                      I should modify all nested functions

                      Only nested functions (not inline functions) that are affected by this issue. The issue is that the variables go out of scope in certain circumstances. For example if you are calling something like Engine.showYesNoWindow() The callback function that you pass to that function will be triggered after any local variables have already been destroyed.

                      So it's basically anywhere you are using a callback function. A few examples would be:

                      Engine.showYesNoWindow()
                      Server.downloadFile()
                      File.extractZipFile()
                      FileSystem.browse()
                      FileSystem.browserForDirectory()

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

                      ? 1 Reply Last reply Reply Quote 2
                      • ?
                        A Former User @d.healey
                        last edited by A Former User

                        I am just trying to understand the case about what to do here. I have a script like below and I am getting Cannot define local variables outside of inline functions or callbacks. error in Hise - the current develop branch.

                        inline function onMYGENERICFUNCControl(component, value)
                        {
                              local myLocalOne = Engine.getSystemTime();  
                              local myLocalTwo = Engine.getVersion();
                              local myLocalThree = Engine.showYesNoWindow(); 
                                
                            if(value)
                            {
                              // Execute a function
                        
                            }
                        
                        
                            else
                            {
                              // Execute a function
                        
                            }
                        };
                        
                        Content.getComponent("MYGENERICFUNC").setControlCallback(onMYGENERICFUNCControl);
                        

                        So Should I transform the local variables with namespace & reg combination like this? Is this an effective usage?

                        namespace myNamespaceFunc
                        {	
                        	reg myLocalOne;
                        	reg myLocalTwo;
                        	reg myLocalThree;
                        			
                        	inline function onMYGENERICFUNCControl(component, value)
                        	{	
                        	    myLocalOne = Engine.getSystemTime();  
                        	    myLocalTwo = Engine.getVersion();
                        	    myLocalThree = Engine.showYesNoWindow(); 
                        	        	
                        	    if(value)
                        	    {
                        	      // Execute a function
                        	
                        	    }
                        	
                        		
                        	    else
                        	    {
                        	      // Execute a function
                        	
                        	    }
                        		
                        	};
                        		Content.getComponent("MYGENERICFUNC").setControlCallback(onMYGENERICFUNCControl);
                        	
                        };
                        
                        d.healeyD 1 Reply Last reply Reply Quote 0
                        • d.healeyD
                          d.healey @A Former User
                          last edited by

                          @Steve-Mohican You shouldn't be getting an error there, that is an inline function. Are you sure that is where the error is being generated?

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

                          ? 1 Reply Last reply Reply Quote 0
                          • ?
                            A Former User @d.healey
                            last edited by A Former User

                            @d-healey Yes I am getting that error exactly there.

                            I am also using a Server.callWithGET() inside the if statement and using these 3 variables in the if statement but I just trimmed the code to make it more clean

                            d.healeyD 1 Reply Last reply Reply Quote 0
                            • d.healeyD
                              d.healey @A Former User
                              last edited by d.healey

                              @Steve-Mohican Aha now it's making more sense. The Server.callWithGET() function takes a callback function as its last parameter. Within this inner function you cannot declare local variables or use local variables declared outside of this function.

                              For this you should make a reg object at the top of your namespace called nest, and use this object in place of your local variables. For example:

                              Don't do this

                              namespace myNamespace
                              {
                                  inline function myFunc()
                                  {
                                      local myVar1 = 10;
                                      local myVar2 = 20;
                              
                                      Server.callWithGET("google.com", {}, function()
                                      {
                                          Console.print(myVar1); // myVar1 doesn't exist here so you will get an error
                                      });
                                  }
                              }
                              

                              Do this:

                              namespace myNamespace
                              {
                                  reg nest = {};
                              
                                  inline function myFunc()
                                  {
                                      nest.myVar1 = 10;
                                      nest.myVar2 = 20;
                              
                                      Server.callWithGET("google.com", {}, function()
                                      {
                                          Console.print(nest.myVar1);
                                      });
                                  }
                              }
                              

                              @Christoph-Hart As a longer term solution is it possible to do something behind the scenes that allows local variables to exist within the inner functions? Maybe internally they are converted to reg variables...

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

                              ? 1 Reply Last reply Reply Quote 1
                              • ?
                                A Former User @d.healey
                                last edited by

                                @d-healey Wow, very different solution. Thank you for the suggestion, I will check!

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

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

                                  In your case you might get away with just changing local to var for the origin definition.

                                  I'm finding this approach is causing problems in other functions that use the same variable names. My nest solution is still working fine but it's ugly code. Is there a possibility of some syntactic sugar being added to HISE so that we can use the word local in these scenarios and internally HISE will treat them as children of a reg variable, just like I'm doing with my reg nest method?

                                  Edit: I just realised I asked this already :p

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

                                  1 Reply Last reply Reply Quote 0
                                  • A
                                    andioak
                                    last edited by andioak

                                    @Christoph-Hart @d-healey It's been a while for me here at the Hise forums, so please correct me if I'm wrong, but isn't one important issue the fact that we're using "inline" functions?

                                    Perhaps we could be using normal functions (not "inline") for things that are asynchronous in nature, such as the fetching of files through the FileSystem.browse() object. If the "inline" is the issue that makes the local variables go out of scope, when the stack is not synchronously executed as per the inline function specification. (if I'm getting that bit right :beaming_face_with_smiling_eyes: )

                                    After all, the inlining of functionality is for things that are very sensitive in timing, real-time stuff for audio and such things. This is not going to run in the same threads as the audio streaming, right?

                                    Otherwise, the "asynchrounous:ness" of the file browser could perhaps be explained a bit, I would love some info on that. Looking at the docs atm :)

                                    (a novice at C++ at best still :) )

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

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

                                      @Christoph-Hart @d-healey It's been a while for me here at the Hise forums, so please correct me if I'm wrong, but isn't one important issue the fact that we're using "inline" functions?

                                      The particular issue in this thread is when you use a non-inline function within and inline function. This is often the case when using things like the file browser which requires a callback function as a parameter.

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

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

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

                                        The particular issue in this thread is when you use a non-inline function within and inline function. This is often the case when using things like the file browser which requires a callback function as a parameter.

                                        @d-healey Yes, I know. But why use an inline function to begin with in the case of the file browser? (or anything asynchronous in nature) Is not better to use a normal function in the cases that require asynchronous behaviour?

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

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

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

                                          Christoph HartC 2 Replies Last reply Reply Quote 1
                                          • 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
                                            • First post
                                              Last post

                                            48

                                            Online

                                            1.7k

                                            Users

                                            11.7k

                                            Topics

                                            101.8k

                                            Posts