HISE Logo Forum
    • Categories
    • Register
    • Login

    Iterator variable clashes

    Scheduled Pinned Locked Moved Bug Reports
    3 Posts 2 Posters 747 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
      last edited by

      I've noticed a problem if you have a call to a function that contains a loop within another loop and they both use the same variable name for their iterator. It would be good if there was some sort of scope for iterator variables to prevent these clashes, the current solution is to use different names for the iterators but I have a number of library functions that use loops and it's not always easy to keep track of which iterator names are "safe" for each function call.

      Demo:

      //Includes 
      
      //Init
      
      //Functions
      inline function doSomething()
      {
      	for (i = 0; i < 10; i++)
      	{
      		Console.print("Function: " + i);
      	}
      }
      
      //Callbacks
      function onNoteOn()
      {
      	for (i = 0; i < 5; i ++)
      	{
      		doSomething();
      	}
      }
      
      function onNoteOff()
      {
      }
      
      function onController()
      {
      }
      
      function onTimer()
      {	
      }
      
      function onControl(number, value)
      {
      }
      

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

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

        Yes this is odd. I need to check the code (the default JUCE javascript implementation is working correctly).

        There is a workaround though:

        inline function doSomething()
        {
        	local i;
        	
        	for (i = 0; i < 10; i++) // i is now `local` and not `var`
        	{
        		Console.print("Function: " + i);
        	}
        }
        
        for (i = 0; i < 5; i ++)
        {
        	Console.print("Outer: " + i);
        	doSomething();
        }
        

        behaves like expected (the only difference is the local i declaration in the inline function. local variables are scoped to their inline function lifespan so this would be the obvious choice here.
        I could tell the parser to accept a local declaration within the for initializer to make it a little bit less awkward.

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

          Yep that will work :)

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

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

          47

          Online

          1.7k

          Users

          11.7k

          Topics

          101.9k

          Posts