ScalaPB
ScalaPB copied to clipboard
Support new Scala 3 syntax
scalapb generated code fails to compile in Scala 3 under these scalac flags:
-new-syntax- Requirethenanddoin control expressions.-source:future- Deprecates: private[this],_for wildcard arguments, some other things-Werror- Fail the compilation if there are any warnings.
Would it be possible to have a new code generator under Scala 3 which would fix these problems (some of them will have to be fixed sooner or later)? I can make a PR if it helps (I'll probably need a little guidance.)
I assume the reason this change is needed since you have a single project that includes both your code and generated code (correct me if I'm wrong). A possible solution which I highly recommend, regardless of this problem, is to move the code generation in a dedicated sub-project within the same SBT build, and have the main project depend on it. Currently, the generator aims to generate code that works for both Scala 2.x and 3.x. This reduces the effort needed to keep the code updated. I would defer on satisfying the above flags until they are required by default by scalac.
You are correct. Another sub-project would solve it.
According to Wildcard Arguments in Types:
In Scala 3.2, the meaning of _ changes from wildcard to placeholder for type parameter.
We are already at Scala 3.1.1 so it won't take long (assuming they stick to that schedule) for these changes to be necessary.
Yes, maybe having the code generator have cases for different language versions is going to be inevitable one day. Is there an instance where the generated code is impacted by wildcard arguments in types?
Using the Address Book example from the docs and the -source:future, -Werror flags the following errors are reported regarding the wildcard arguments:
[error] -- Error: (...)/modules/shared/.jvm/target/scala-3.1.1/src_managed/main/scalapb/tutorial/addressbook/AddressBook.scala:98:107
[error] 98 | def messageCompanionForFieldNumber(__number: _root_.scala.Int): _root_.scalapb.GeneratedMessageCompanion[_] = {
[error] | ^
[error] | `_` is deprecated for wildcard arguments of types: use `?` instead
[error] -- Error: (...)/modules/shared/.jvm/target/scala-3.1.1/src_managed/main/scalapb/tutorial/addressbook/AddressBook.scala:99:56
[error] 99 | var __out: _root_.scalapb.GeneratedMessageCompanion[_] = null
[error] | ^
[error] | `_` is deprecated for wildcard arguments of types: use `?` instead
[error] -- Error: (...)/modules/shared/.jvm/target/scala-3.1.1/src_managed/main/scalapb/tutorial/addressbook/AddressBook.scala:105:82
[error] 105 | lazy val nestedMessagesCompanions: Seq[_root_.scalapb.GeneratedMessageCompanion[_ <: _root_.scalapb.GeneratedMessage]] = Seq.empty
[error] | ^
[error] | `_` is deprecated for wildcard arguments of types: use `?` instead
[error] -- Error: (...)/modules/shared/.jvm/target/scala-3.1.1/src_managed/main/scalapb/tutorial/addressbook/AddressBook.scala:106:106
[error] 106 | def enumCompanionForFieldNumber(__fieldNumber: _root_.scala.Int): _root_.scalapb.GeneratedEnumCompanion[_] = throw new MatchError(__fieldNumber)
[error] | ^
[error] | `_` is deprecated for wildcard arguments of types: use `?` instead
With the same repeated for the other classes.
I have some experience with Scala 3 new features I can help if needed.
With the recent release of Scala 3.3.0, it's probably time to revisit this issue. I can work on a PR if it is something @thesamet agrees with.
Sounds like it's time for this future. I am not sure how much different the code should be : is it just the wildcard change? If the changes are fairly small, maybe a generator parameter for Scala 3 syntax would be sufficient.
Yes, there are only a handful of small changes.
@Lasering @thesamet Would be great if you can take a look at https://github.com/scalapb/ScalaPB/pull/1565
Will be released with 0.11.14.