Bug? Current declaration does not match previous one.

Discussion in 'Bugs & wishlist' started by RandomCoder, Sep 4, 2015.

  1. RandomCoder

    RandomCoder Well-Known Member Licensed User

    Sample code to re-create the problem...
    Code:
    Sub Activity_Create(FirstTime As Boolean)
        
    For X = 0 To 5
            
    Log(X)
        
    Next
       
        
    For Y = 0 To  5
            
    Dim X As Int = Y
            
    Log(X)
        
    Next
    End Sub
    NOTE: Only happens when trying to run in 'Rapid Debug' mode.

    In Rapid Debug this code will fail to compile with the following error message
    In both Legacy Debug and Release mode it will compile without error and run just fine on the device.

    PS. I'm pretty careful to always fully declare my variables and only fell foul of this whilst modifying someone else's code. The sample above is meaningless and purely for the purpose of recreating the error.
     
  2. LucaMs

    LucaMs Expert Licensed User

    If I'm not mistaken, if you do not declare the variable type, B4A implies String.
    The second declaration is equivalent to a Redim (VB.net) but you have changed the type to Int.

    As you rightly wrote, it is better to explicitly write the type.
     
    wonder likes this.
  3. LucaMs

    LucaMs Expert Licensed User

    Code:
    Dim X As Int
        
    For X = 0 To 5
            
    Log(X)
        
    Next
      
        
    For Y = 0 To  5
            
    Dim X As Int = Y
            
    Log(X)
        
    Next
    If you try this (added only the first line) there will be no errors.
     
    wonder likes this.
  4. RandomCoder

    RandomCoder Well-Known Member Licensed User

    Hi @LucaMs I realise that declaring the type before the first For loop will cure the problem and this is how I usually code. But my concern was that the problem is only hightlighted in Rapid Debug mode. The other methods of compiling work without error.

    It's also interesting that if you change the "Int" in Dim X As Int = Y to "String" or "Double" or "Object" it still throws errors. I wonder what X is initially defined as when used in the For loop without explicitly declaring the type?
     
  5. moster67

    moster67 Expert Licensed User

    If I recall correctly, the control variable, X in this case, always defaults to Int in a for statement unless you declare it differently. This means that X in most cases is Int in for statements and also in this example.
     
  6. RandomCoder

    RandomCoder Well-Known Member Licensed User

    I can agree with that as changing the type when declaring X in the second For loop throws a cast type error. Also it is quite happy to compile in Release and Legacy Debug. The problem is with Rapid Debug where it seems to be a little over zealous.
     
  7. LucaMs

    LucaMs Expert Licensed User

    Yes, I'd understand; you've noticed that there is no "consistency".


    Yes, you're right. I have a motto: "Try" :); so I tried and GetType (x) returns "java.lang.Integer" (as default).
    If you do not define the type of a parameter of a routine, B4A uses String. I supposed it was the same for variables (though I doubted: a loop with a String as counter? :))
     
  8. RandomCoder

    RandomCoder Well-Known Member Licensed User

    Well I never knew there was a GetType Method...
    GetType (object As Object) As String
    Returns a string representing the object's java type.

    Time for bed now, but at least I can go to sleep knowing that I have learnt something new today :D
     
    DonManfred and LucaMs like this.
  9. LucaMs

    LucaMs Expert Licensed User

    I, too, should go to sleep; instead I'm going to write a post that will be hated and controversial :p
     
    Derek Johnson and RandomCoder like this.
  10. Erel

    Erel Administrator Staff Member Licensed User

    The type of undeclared variables in For loops is Int.

    http://www.b4x.com/android/forum/pages/results/?query=for

    The issue you encounter here is indeed specific to the rapid debugger. The error message tells you how to fix it. You need to declare the variable before the first for loop and it will work.

    It is difficult to explain the reason for this error without writing several pages that explain how the rapid debugger works. It is related to an optimization done by the compiler in the first loop.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice