Page 1 of 1

Strict compilation mode

Posted: Mon Aug 17, 2020 11:12 pm
by anmldr

Code: Select all

on mouseUp
  // local tAdd
   put 1 into tAdd
   repeat with x = 1 to 10
      add 1 to tAdd
   end repeat
end mouseUp
This is just an example. If I have strict compilation mode selected in Preferences then there is an error noted at "put 1 into tAdd". That is expected. If I remove the "//" then it is declared and no error. This is also expected behavior.

However, WHY is there never an error on the "repeat with x = 1 to 10" line? Isn't "x" a local variable also in the script above? Why does it not need to be declared?

Linda

Re: Strict compilation mode

Posted: Tue Aug 18, 2020 3:28 am
by AlbertAA
Not having to declare local variables explicitly is one of the many features/bugs or strengths/weaknesses of LiveCode.
I advise to remove the "//" (which caused all your problems), and also to remove the "local tAdd" (which you do not need).
Best luck

Re: Strict compilation mode

Posted: Tue Aug 18, 2020 3:31 am
by dunbarx
Hi.

"x' is indeed "declared" by virtue of creating the "repeat with" control structure. It is just how LC works. It is a good thing.

Craig

Re: Strict compilation mode

Posted: Tue Aug 18, 2020 3:35 am
by dunbarx
Albert.
Not having to declare local variables explicitly is one of the many features/bugs or strengths/weaknesses of LiveCode.
Yes, but in strict compilation mode LC becomes a bit more like other languages, in that declarations must be explicitly made prior to using a variable in any way. LC will throw and error if you do not. I never use it. Old fashioned, you know.

Craig

Re: Strict compilation mode

Posted: Tue Aug 18, 2020 10:52 am
by bogs
anmldr wrote:
Mon Aug 17, 2020 11:12 pm
However, WHY is there never an error on the "repeat with x = 1 to 10" line? Isn't "x" a local variable also in the script above? Why does it not need to be declared?
Not exactly. Repeat is a control structure, and the conditions are predefined already by the structure.
Dictionary -
The condition is any expression that evaluates to true or false.
The number, startValue, endValue, and increment are numbers or expressions that evaluate to numbers.
The counter or labelVariable is a legal variable name.
The chunkType is one of character (or char), word, line, item, or token.
The container is any existing container. The array is any existing container that contains an array of values.
The statementList consists of one or more LiveCode statements, and can also include if, switch, try, or repeat control structures.


The with form:
The with counter = startValue to endValue form and the with counter = startValue down to endValue form set the counter to the startValue at the beginning of the loop, and increase (or decrease, if you're using the down to form) the countVariable by 1 each time through the loop. When the counter is greater than or equal to the endValue, (less than or equal to, if you're using the down to form), the loop performs its final iteration and then ends.
in the case of a repeat loop, x is not a variable being declared/initiated by you.