rokucommunity / brighterscript

A superset of Roku's BrightScript language
MIT License
152 stars 47 forks source link

Null-coalescing transpilation can cause problems in print statements #1194

Open markwpearce opened 1 month ago

markwpearce commented 1 month ago

Consider:

  print "Values:" data.values ?? []

transpiles to:

    print "Values:" (function(data)
            __bsConsequent = data.values
            if __bsConsequent <> invalid then
                return __bsConsequent
            else
                return []
            end if
        end function)(data)

this transpilation causes a run-time error: Function Call Operator ( ) attempted on non-function. (runtime error &he0)

however, if it is preceded with a semi-colon, then it does not have any problem.

  print "Values:"; data.values ?? []

gives:

   print "Values:"; (function(data)
            __bsConsequent = data.values
            if __bsConsequent <> invalid then
                return __bsConsequent
            else
                return []
            end if
        end function)(data)

If any self-invoking function in transpilation (eg. null-coalescing, ternary, etc.) is in a print statement.. it's probably best to precede it with a semi-colon.

markwpearce commented 1 month ago

Alternately, we could just ALWAYS insert a semicolon between parts of a print statement... I don't think that would hurt anything.