question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using !{}-blocks adds extra semicola (empty statements); prevents break and continue from working

See original GitHub issue

I recently started using JTE (great solution, btw) and while experimenting with loops, it struck me that there is no obvious way to prematurely exit a loop, so I resorted to using !{break;}. However, this generated break;;, which makes the compiler complain that the empty statement followed by the break; is unreachable.

I see two possible solutions to this problem:

  1. Make the compiler (precompiler, really?) remove the erroneous semicolon after single-keyword statements such as break; and continue; which has shown the same behaviour in my (very shallow) research, so that code like this would compile correctly:
    @for (int i = 0; i < 100; i++)
        @if (i  % 2 == 0) !{continue;} @endif
        ${i}
    @endfor
    
    Please note that I have not yet tracked down where the empty statement gets generated and I sadly currently do not have the resources for that.
  2. Add new @-keywords for such operations (e.g., @break, @continue), which would allow such exit statements to blend in nicely into the rest of the JTE control structures, for example:
    @for (int i = 0; i < 100; i++)
        @if (i  % 2 == 0) @continue @endif
        ${i}
    @endfor
    

Interestingly enough, specifying a loop label using !{label:} @for (/* stuff */) and then using !{continue label;} triggers the same unreachable statement error.

Doing some research, I noticed that the extra semicola get generated for every instruction inside !{}-blocks, even for aforementioned loop labels, which obviously causes trouble. Removing the semicola at the end of such instructions inside JTE code makes the IntelliJ plugin complain that a “; was expected” but otherwise lets the template render correctly.

However, using labels as stated above won’t even work if removing the semicolon from the break or continue instruction because the label applies to an empty statement always, which makes the label undefined and thus invalid to use with break or continue; also, it wouldn’t even correctly apply to loops.

Given that even the documentation shows that !{}-blocks should be closed with a semicolon (which simply feels right, this is plain Java after all), it is an annoyance that there is a mismatch between the IntelliJ plugin (reporting errors) and the compiler (correctly but naively “fixing” the statement).

Could the fix be as simple as checking whether the statement string terminates with ; and writing a semicolon otherwise to the generated Java source file? (Obviously, this wouldn’t work for labels unless new labelling keywords are introduced into JTE, but some sacrifices have to be made, I guess.)

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
kelunikcommented, Sep 21, 2022

@casid We should probably open another issue for the issue mentioned in https://github.com/casid/jte/issues/174#issuecomment-1253324260? The second variant isn’t something I’d like to read.

1reaction
casidcommented, Sep 20, 2022

Thank you, that’s great to hear!

And yes, that should enable loop labeling as well.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Conditionals and Loops - Introduction to Programming in Java
Labeled break and continue statements. The break and continue statements apply to the innermost for or while loop. Sometimes we want to jump...
Read more >
8. Compound statements — Python 3.11.1 documentation
A continue statement executed in the first suite skips the rest of the suite and continues with the next item, or with the...
Read more >
How To Use Break, Continue, and Pass Statements when ...
In this tutorial, we will go over the break, continue, and pass statements in Python, which will allow you to use for and...
Read more >
AL Control Statements - Business Central | Microsoft Learn
In AL, a semicolon is used to separate statements and not to terminate them, as in other programming languages. Nevertheless, an extra semicolon...
Read more >
Purpose of while(1); statement in C - Stack Overflow
I am aware while(1) (no semicolon) loops infinitely and is similar to a spinlock situation. However I do not see where while(1); could...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found