Using !{}-blocks adds extra semicola (empty statements); prevents break and continue from working
See original GitHub issueI 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:
- Make the compiler (precompiler, really?) remove the erroneous semicolon after single-keyword statements such as
break;
andcontinue;
which has shown the same behaviour in my (very shallow) research, so that code like this would compile correctly:
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.@for (int i = 0; i < 100; i++) @if (i % 2 == 0) !{continue;} @endif ${i} @endfor
- 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:
- Created a year ago
- Comments:7 (4 by maintainers)
@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.
Thank you, that’s great to hear!
And yes, that should enable loop labeling as well.