Caution when using Next Repeat

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Simon Knight
Posts: 854
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Caution when using Next Repeat

Post by Simon Knight » Sat Apr 09, 2022 1:11 pm

I have just wasted a morning trying to discover why the following code failed:

Code: Select all

if (there is file tNewFilePath) then
      -- skip this file
      put "New name already exists, so skipping new name  : " & tNewFileName & cr after tErrorsLog
      add one to tErrorCount
      next repeat
   end if
The snip was part of a file renaming handler that would run for thousands of files and then stop with an error in the IDE. Several things were odd: the error message in the IDE claimed there was a problem with Character 1 of the first line (if there is a file tNewFilePath then), when wrapped in a Try Catch End Try the IDE reported the error on wherever the try statement was placed above the code snip (note there were quite a few lines before the code snip) and the catch statement did not catch any error.

After much head scratching I realised the issue was with the line "next repeat". This line is an orphan as I removed the original repeat loop and failed to notice the line. The error only occurred when the if statement was true which was a rare event and the reason why the rest of the code processed thousands of files before failing.

I think the IDE should post a syntax error and not allow the code to be applied much as it does with if statements with no end ifs - what do you think?

S
best wishes
Skids

stam
Posts: 2682
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Caution when using Next Repeat

Post by stam » Sat Apr 09, 2022 1:42 pm

I agree the IDE should post a warning if a next repeat statement is used outside a of a repeat loop... may be worth posting a bug report/quality improvement request.

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: Caution when using Next Repeat

Post by mwieder » Sun Apr 10, 2022 2:48 am

Definitely bug-worthy behavior.
I'm not even sure what to expect when an orphan next repeat command would get executed.

Simon Knight
Posts: 854
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Re: Caution when using Next Repeat

Post by Simon Knight » Sun Apr 10, 2022 11:51 am

"I'm not even sure what to expect when an orphan next repeat command would get executed."

A slightly unhelpful error occurs in the IDE at the start of the block that contains the line. I have not tried it in a built application but I suspect that it will be a silent fail as no error is trapped within a try, catch,end try code block.

S
best wishes
Skids

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9833
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Caution when using Next Repeat

Post by FourthWorld » Sun Apr 10, 2022 8:35 pm

Simon Knight wrote:
Sun Apr 10, 2022 11:51 am
A slightly unhelpful error occurs in the IDE at the start of the block that contains the line. I have not tried it in a built application but I suspect that it will be a silent fail as no error is trapped within a try, catch,end try code block.
Errors are reported by the engine in at least three different ways, depending on the nature of the error.

What is the message you see in the IDE, and where does the IDE present this message?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: Caution when using Next Repeat

Post by mwieder » Sun Apr 10, 2022 8:57 pm

So... interestingly, the code compiles with no problems (no context checking).
Executing this code does throw an error at the 'next repeat' line.
Placing the 'next repeat' in a try/catch construct throws the same error but earmarks it at the enclosing 'try' line.
And placing that in an if construct moves the error report to the outermost conditional.

Code: Select all

local tVariable
   if tVariable is empty then
      try
         next repeat
      catch e
         answer e
      end try
   end if
In an ideal world (looking at you, 10.0) the editor would catch this in the edit phase, the way it does if I enter "end repeat" instead of "next repeat".

stam
Posts: 2682
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Caution when using Next Repeat

Post by stam » Sun Apr 10, 2022 9:15 pm

It seems like something that should be reported by the IDE before run time.
All good and well hoping that v10.0 will fix this, but if a bug report doesn't exist for this already maybe you could submit a bug/feature request?

Simon Knight
Posts: 854
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Re: Caution when using Next Repeat

Post by Simon Knight » Mon Apr 11, 2022 9:07 am

Having prepared a demo stack for my bug report I did the search of the quality control site. The issue was first reported in 2015 and has been re-reported several times since. Seems that there are also issues with exit as well.

Anyhow I attach my demo stack so that others may enjoy the problem ;-)

S
NextRepeatError.livecode.zip
Demo of orphan Next Repeat failure
(1.59 KiB) Downloaded 65 times
best wishes
Skids

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9833
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Caution when using Next Repeat

Post by FourthWorld » Mon Apr 11, 2022 2:52 pm

mwieder wrote:
Sun Apr 10, 2022 8:57 pm
Executing this code does throw an error at the 'next repeat' line.
What is the error message?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Simon Knight
Posts: 854
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Re: Caution when using Next Repeat

Post by Simon Knight » Mon Apr 11, 2022 3:29 pm

The only message I saw was error at line n character 1.

S
best wishes
Skids

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9833
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Caution when using Next Repeat

Post by FourthWorld » Mon Apr 11, 2022 4:37 pm

Simon Knight wrote:
Mon Apr 11, 2022 3:29 pm
The only message I saw was error at line n character 1.
Was n the correct line?

Vague, yes, and I'd love to see more Clang-like error assistance. But it's useful as a starting point to assess if the engine is at least covering the basics.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Simon Knight
Posts: 854
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Re: Caution when using Next Repeat

Post by Simon Knight » Mon Apr 11, 2022 5:11 pm

Hi Richard,

Was line n correct ? No. The code stops in the IDE on the top level structure, meaning that if there is a TRY statement it stops and reports the line the Try is on. Try my demo stack above to see. In effect the code executes down to the line "next repeat" then jumps up to the Try or the If statement and reports the error.

S
best wishes
Skids

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9833
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Caution when using Next Repeat

Post by FourthWorld » Mon Apr 11, 2022 5:39 pm

Simon Knight wrote:
Mon Apr 11, 2022 5:11 pm
Hi Richard,

Was line n correct ? No. The code stops in the IDE on the top level structure, meaning that if there is a TRY statement it stops and reports the line the Try is on.
The try control structure alters much with exception handing, by its nature. I suppose it could be further refined to provide more context sensitivity, but the inner workings of try seem tangential to the core concern here about orphaned loop closures.

In the original code, before try was attempted as a remedy, was n the appropriate line number?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Simon Knight
Posts: 854
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Re: Caution when using Next Repeat

Post by Simon Knight » Mon Apr 11, 2022 6:05 pm

Hi Richard,

Short answer : No.

Long answer, please down load my demo stack which is posted above which will answer many of your questions. Suffice to say the error line reported was never the problem line. When the line "Next return" was within an IF structure the overall app would run until the line was actioned based on the IF structure. When hit the error was incorrectly reported it as occurring on the line of the IF statement that contained the line "Next Return". When wrapped in a TRY CATCH the error is reported on the line of the TRY statement.

S
best wishes
Skids

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: Caution when using Next Repeat

Post by mwieder » Mon Apr 11, 2022 6:11 pm

...and the reported error is 241: "error in statement". Can't get much more generic than that.
The more glaring problem, I think, is reporting the error on the outermost conditional.

Post Reply

Return to “Talking LiveCode”