congo-cc / congo-parser-generator

The CongoCC Parser Generator, the Next Generation of JavaCC 21, which in turn was the next generation of JavaCC
https://discuss.congocc.org/
Other
36 stars 11 forks source link

[java 17] Replace this instanceof check and cast with 'instanceof StringBuilder stringbuilder' #25

Closed stbischof closed 1 year ago

stbischof commented 1 year ago
 if(content instanceof StringBuilder sbContent){
 /// use sbContent
}

https://github.com/congo-cc/congo-parser-generator/blob/08708399b324504961bb1a55fcee58f31e5a1d2c/src/templates/java/TokenSource.java.ftl#L105-L111

revusky commented 1 year ago

Well, the style checker is right. This is a cleaner way of writing it. However, unfortunately, we cannot use the suggestion because this construct is only a stable feature of Java since JDK 16. Our coe generation is targeting JDK 8 for now, but if we were going to target a later version, we would probably go to JDK 11, which is the next LTS release after JDK 8. And that still does not have this language feaature. (The next LTS release after that is 17, which does have this as a stable feature.)

stbischof commented 1 year ago

I expected the jdk in org/congocc/app/AppSettings

could be checked/used in the template file.

Would that make everything to complex?

revusky commented 1 year ago

Well, that is not such an unreasonable thing to expect, but it is not really being used very much. So, yes, it is possible to have a condition in the template and generate the nicer, more modern code if the target jdk is set high enough. But, generally speaking, it's not really worth bothering with very much. You end up with two versions of the code in your template and... It's generally more pragmatic just to generate one version of the code, the one that always works!