14.1 Troubleshooting

This section covers some common problems and presents their solutions.

  1. Table initialization: Be careful when trying to create a table field by assigning a variable to it that has not been used before, i.e. is Nil. If you do that, the table field will not be created. For example, the following will not work:

    t = {}    ; create a table
    t.x = y   ; assign 'y' to field x; note that y is Nil
    DebugPrint(t.x)  ; ---> Error! Field 'x' not initialized!

    The solution is to initialize y first, e.g.:

    t = {}    ; create a table
    y = 0     ; set y to 0
    t.x = y   ; assign 'y' to field x
    DebugPrint(t.x)  ; Works! Prints '0'

  2. Checking a variable against Nil: Be careful when checking a variable against Nil! GetType() is the only reliable way to find out if a variable is Nil or not. Checking the variable against the Nil identifier is not a good idea because that would also result in True if the variable was zero instead of Nil. Example:

    a = 0
    b = Nil
    DebugPrint(GetType(a) = #NIL, a = Nil)  ; prints "0 1"
    DebugPrint(GetType(b) = #NIL, b = Nil)  ; prints "1 1"

    You see that "a = Nil" returns True although a is zero. That is because Nil is always regarded as zero when used in expressions. Thus, if you want to find out whether a variable really is Nil, use GetType() and compare the result against #NIL. Starting with Hollywood 6.0 you can also use the dedicated IsNil() function to check a variable against Nil. See IsNil for details.

  3. Wrong variable initialization: In Hollywood initializing multiple variables is a bit different than in most other languages because Hollywood expects only one equal sign. For example, this might look correct but it is wrong:

    ; bad code!
    Local a = 5, b = 6, c = 7

    Unfortunately, this bad code would not even trigger an error but it would be interpreted in a wrong way. The code above would make Hollywood assign 5 to a and simply drop the rest because there is only one variable on the left side of the equal sign. So you have to be careful with multiple variable initialization. Accordingly, the correct version would be the following code:

    ; good code!
    Local a, b, c = 5, 6, 7

    This code will assign 5 to a, 6 to b, and 7 to c.

  4. Returning values: Be careful with functions that return something. The return value has to be enclosed in parentheses. Code like this is wrong and does not trigger an error:

    Function p_Add(a, b)
        Local r = a + b
        Return r   ; OUCH!!!

    This code would be interpreted as "Return, and then call the function r()". Of course, the call to r() will never be reached but the function as written above will return Nil, i.e. nothing, in every case. The correct version is this:

    Function p_Add(a, b)
        Local r = a + b

Show TOC