如何在 Scala 中允许 @throws 用于 case 类?

How to allow @throws for case classes in Scala?

我有旧的 Scala 代码可以从 2.13.3 版本升级到 2.13.8 版本。在升级过程中,我遇到了以下错误:

On line 2: error: `@throws` only allowed for methods and constructors

由于 case class 不允许 @throws 而发生错误。代码如下:

scala> @throws[IllegalArgumentException]
     | final case class MyClass(name: String, age: Int)
       final case class MyClass(name: String, age: Int)
                        ^
On line 2: error: `@throws` only allowed for methods and constructors

因此,我如何在 Scala 2.13.8 中允许 @throws 用于 case class

这似乎适用于我的 2.13 REPL:

scala
Welcome to Scala 2.13.8 (OpenJDK 64-Bit Server VM, Java 11.0.15).
Type in expressions for evaluation. Or try :help.

scala> final case class MyClass @throws(classOf[IllegalArgumentException]) (name: String, age: Int)
class MyClass

对于其他好奇的人,这种行为似乎在 2.13.5 中发生了变化:https://github.com/scala/scala/pull/9465