Java records in mixed Java/Scala 3 project

I see that support for Java records was added in 2.13.7. However, I’m still running into troubles in 3.1.1 (sbt 1.6.2):

[error] -- Error: /.../src/main/java/Foo.java:1:14 ------------------
[error] 1 |public record Foo(int value) {
[error]   |              ^^^
[error]   |              illegal start of type declaration

Is this something that’s supposed to be working? I couldn’t find a related issue on GitHub.
Note that the code works in 2.13.8, and also in 3.1.1 if all the Scala code is removed (i.e., as a pure Java project).

They are different codebases evolving at different rates in different directions, so I think that hasn’t been forward-ported yet.

Understood. Do you know if there is a ticket somewhere I can track? Records were finalized in Java 16 (and 18 is out!). It’s only been a year, but it’s a bit annoying not to have them. (I have several projects in Java with tests in Scala, so I may care more about Java support than other Scala developers.)

I’ve opened Support parsing record classes in Java source files · Issue #14846 · lampepfl/dotty · GitHub.

1 Like

There were PRs for triple quote strings and records. I’ll start with a swing at triple quotes, in case that is easier. As usual, the hard part is the test.

Hey, Can you run your program on this online java compiler - Online Java Compiler | Java IDE Online - InterviewBit and save your snippet and share it here, So we will check the whole error?

The question is how scalac parses java source in mixed compilation. Otherwise, scalac assumes the available javac consumes java source.

1 Like

There is an open PR on this at Support records in JavaParsers by TheElectronWill · Pull Request #16762 · lampepfl/dotty · GitHub , hopefully it will cross the finish line eventually.

If you are a user who is running into the lack of this, consider changing your sbt project to set compileOrder to use CompileOrder.JavaThenScala (or, if appropriate for your code, CompileOrder.ScalaThenJava) instead of the default CompileOrder.Mixed. This will tell the Scala compiler not to attempt to process the Java sources.

see scala-sbt.org/1.x/docs/Java-Sources.html

when somebody on Discord hit this today, the error message from the compiler was “illegal start of type declaration”, pointing to interface in public sealed interface in a Java source file

2 Likes

The Pull Request has now been merged! Scala 3.3.1 will include the fix, but you can already use the nighly version.

2 Likes

I think I’m still hitting this issue with sealed interfaces. I reproduced the issue with 3.3.1 here: GitHub - miguel-vila/scala-3-java-sealed-interface-issue: reproduction of an issue compiling both scala 3 and java sealed interfaces Setting the order to JavaThenScala does avoid it, tho.

There is a ticket for Java sealed parsed by Scala 3.

Support trickled into Scala 2 last year.

1 Like