如何在 Scala class 定义中使用名为 'type' 的变量?

How to use a variable named 'type' in Scala class definition?

我必须创建一个 class,在其定义中包含一个名为 'type' 的变量,例如:

case class testEntity (
   type: String,
   val: String
)

有没有办法做到这一点?

要使用通常不允许的名称,您必须将它们放在反引号 (`) 中。 您的声明将如下所示:

case class testEntity (
   `type`: String,
   `val`: String
)

因为 typeval 都是关键字。

编辑:Scala 中的约定也是 class 名称以大写字母开头。