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 @Christoph Hart
      last edited by

      @christoph-hart Would using reg or var in place of local be the way to go, or should we place the variable outside of the inline function entirely?

      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 Christoph Hart

        @d-healey This is the same actually - if you define a var inside a inline function, it will get added to the global scope anyway, but sure, writing it in the global scope is a bit cleaner.

        inline function setX()
        {
            //  ugly as f***
            var x = 90;
        }
        
        setX();
        Console.print(x);
        
        d.healeyD 1 Reply Last reply Reply Quote 1
        • d.healeyD
          d.healey @Christoph Hart
          last edited by

          @christoph-hart Would it be possible to pass arbitrary values as parameters into the nested function?

          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 Depends on the context, but if the API call expects a function with a certain amount of parameters, it's impossible. But I can understand the rationale of it to avoid polluting the global namespace with variables that hold internal information.

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

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

              But I can understand the rationale of it to avoid polluting the global namespace with variables that hold internal information.

              Maybe using a single global object for these things would help keep it contained.

              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 d.healey

                reg seems to work, anything wrong with that? Nope, doesn't always work

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

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

                  @Christoph-Hart Now that we have this error message I'm aware of how often I'm doing this kind of thing :p I'm just going to put vars everywhere, to hell with scope, who needs it!

                  alt text

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

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

                    Ouch! that might explain a few things indeed!

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

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

                      What happens if an inline function contains a nested function which calls another inline function and that second inline function contains local variables? I ask because I'm getting some suspicious crashes :)

                      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

                        @d-healey This shouldn't affect this example - it would if you would define that second inline function inside the first one, but that's not possible anyway.

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

                          I'm adding reg nest = {}; at the top of each namespace and putting my nested function variables inside there, not ideal but keeps things tidy.

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

                          1 Reply Last reply Reply Quote 1
                          • FortuneF
                            Fortune @Christoph Hart
                            last edited by

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

                            So the fix is pretty easy: don't allow local references in nested function calls.

                            What about previously finished projects? All of them must be modified now, because you can't fix?

                            Sorry but the above suggestion is not a "fix", it is a prevention workaround. I think rather than telling us to not allowing local references in nested function calls, this issue should be really fixed first. You should fix it.

                            Christoph HartC 1 Reply Last reply Reply Quote 0
                            • Christoph HartC
                              Christoph Hart @Fortune
                              last edited by

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

                              Sorry but the above suggestion is not a "fix", it is a prevention workaround

                              Of course it is a fix. There was a problem in the language that lead to unintentional behaviour so now it throws a proper error and prevents users from facing this issue.

                              And if your finished project doesn't have any issues that come from this problem, you don't need to do anything, but the next time you'll update it and it start throwing these errors, you can fix them and sleep better.

                              DanHD 1 Reply Last reply Reply Quote 4
                              • DanHD
                                DanH @Christoph Hart
                                last edited by

                                @christoph-hart I'm getting an error in my expansion installer callback on the line "local origin = hr;". I think this was largely copied from one of the example projects provided by @Christoph-Hart.

                                Just so I'm crystal clear, do I want to be using something other than 'local'?

                                I've had some users reporting that the expansion installer gets stuck trying to install the samples - I wonder if this is related...

                                inline function onINSTALLEXPControl(component, value)
                                    {
                                        
                                        if (value == 1)
                                        {
                                    
                                  local msg = "SELECT EXPANSION .hr1 FILE";
                                
                                  Engine.showYesNoWindow("SELECT .HR1 FILE", msg, function(response)
                                  {
                                    if (response)
                                    {
                                      FileSystem.browse(FileSystem.Downloads, false, "*.hr1", function(hr)
                                      {
                                        if (hr.toString(2) == ".hr1")
                                        {
                                          local origin = hr; // Scoped copy
                                          msg = "SELECT A LOCATION FOR THE SAMPLE FILES";
                                
                                          Engine.showYesNoWindow("SAMPLE LOCATION", msg, function(response)
                                          {
                                            if (response)
                                            {
                                              FileSystem.browseForDirectory(FileSystem.Samples, function(dir)
                                              {
                                                 expHandler.installExpansionFromPackage(origin, dir);
                                              });
                                            }
                                          });
                                        }
                                      });
                                    }
                                  });
                                }
                                    }
                                
                                Content.getComponent("INSTALLEXP").setControlCallback(onINSTALLEXPControl);
                                

                                DHPlugins / DC Breaks | Artist / Producer / DJ / Developer
                                https://dhplugins.com/ | https://dcbreaks.com/
                                London, UK

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

                                  @danh You need to declare that variable as a reg outside of the function, put it at the top of your script/namespace

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

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

                                    @d-healey ok so just:

                                    reg origin = hr; // Scoped copy
                                    
                                    inline function onINSTALLEXPControl(component, value)
                                        {
                                            
                                            if (value == 1)
                                            {
                                        
                                      local msg = "SELECT EXPANSION .hr1 FILE";
                                    
                                      Engine.showYesNoWindow("SELECT .HR1 FILE", msg, function(response)
                                      {
                                        if (response)
                                        {
                                          FileSystem.browse(FileSystem.Downloads, false, "*.hr1", function(hr)
                                          {
                                            if (hr.toString(2) == ".hr1")
                                            {
                                              
                                              msg = "SELECT A LOCATION FOR THE SAMPLE FILES";
                                    
                                              Engine.showYesNoWindow("SAMPLE LOCATION", msg, function(response)
                                              {
                                                if (response)
                                                {
                                                  FileSystem.browseForDirectory(FileSystem.Samples, function(dir)
                                                  {
                                                     expHandler.installExpansionFromPackage(origin, dir);
                                                  });
                                                }
                                              });
                                            }
                                          });
                                        }
                                      });
                                    }
                                        }
                                    
                                    Content.getComponent("INSTALLEXP").setControlCallback(onINSTALLEXPControl);
                                    

                                    DHPlugins / DC Breaks | Artist / Producer / DJ / Developer
                                    https://dhplugins.com/ | https://dcbreaks.com/
                                    London, UK

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

                                      @DanH yes this is example ticks all boxes for that issue, but I can't guarantee that there is nothing else wrong :)

                                      But there is another code smell - you're declaring the local variable inside the nested function definition, which should not be possible (and I think the recent change will also prevent that, but I'll have to check).

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

                                      DanHD d.healeyD 3 Replies Last reply Reply Quote 0
                                      • DanHD
                                        DanH @Christoph Hart
                                        last edited by DanH

                                        @christoph-hart yes I changed 'local origin = hr;' to 'var origin = hr;' and the errors were gone.

                                        EDIT: It's all going wrong when trying to install an expansion now though :face_with_tears_of_joy:

                                        DHPlugins / DC Breaks | Artist / Producer / DJ / Developer
                                        https://dhplugins.com/ | https://dcbreaks.com/
                                        London, UK

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

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

                                          if (hr.toString(2) == ".hr1")

                                          Magic number alert!!! You should use hr.toString(hr.OnlyExtension)

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

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

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

                                            ok so just:

                                            Yes, but it's no longer scoped now so you can change that comment ;)

                                            Also hr does exist at init so you can't assign the value there, you have to do that within your function like you were before.

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

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

                                            43

                                            Online

                                            1.7k

                                            Users

                                            11.7k

                                            Topics

                                            101.8k

                                            Posts