案例类是否允许使用构造函数?

发布于 2024-09-24 12:27:41 字数 839 浏览 1 评论 0原文

我有一个案例类(让我们将其命名为 Stuff),我希望能够通过扩展特征(将其称为 Marker)在运行时创建匿名子类。下面是 REPL 会话的片段,它说明了我正在尝试执行的操作:

scala> trait Marker
defined trait Marker

scala> case class Stuff(i: Int)
defined class Stuff

scala> val a = Stuff(1)
a: Stuff = Stuff(1)

scala> val b = new Stuff(1) with Marker
b: Stuff with Marker = Stuff(1)

请注意如何使用 Stuff.apply() 实例化 a,而在 b< /code> 的 case 我正在调用 case 类的构造函数。

我的问题是:使用构造函数实例化案例类是否合理?在我看来确实如此,因为案例类通常提供便利,例如 ==、< code>.equals() 和 .hashCode() 都可以。我是否遗漏了任何可能使我正在做的事情成为坏事(TM)的事情?

scala> a == b
res0: Boolean = true

scala> a.equals(b)
res1: Boolean = true

scala> a.hashCode == b.hashCode
res2: Boolean = true

I have a case class (let's name it Stuff) that I want to be able to create anonymous subclasses of at run time by extending a trait (call it Marker). Here's a snippet of a REPL session that illustrates what I'm trying to do:

scala> trait Marker
defined trait Marker

scala> case class Stuff(i: Int)
defined class Stuff

scala> val a = Stuff(1)
a: Stuff = Stuff(1)

scala> val b = new Stuff(1) with Marker
b: Stuff with Marker = Stuff(1)

Note how a is instantiated using Stuff.apply(), while in b's case I'm calling the case class' constructor.

My question is: is instantiating case classes using a constructor kosher? It appears to me that it is, since the usual conveniences provided by case classes, such as ==, .equals(), and .hashCode(), all work. Am I missing anything that would brand what I'm doing a Bad Thing (TM)?

scala> a == b
res0: Boolean = true

scala> a.equals(b)
res1: Boolean = true

scala> a.hashCode == b.hashCode
res2: Boolean = true

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

彼岸花似海 2024-10-01 12:27:41

以下是 Stuff.apply 的实现方式:

object Stuff {
  def apply(i: Int): Stuff = new Stuff(i)
}

因此,使用 new Stuff 没有任何坏处。

Here's how Stuff.apply is implemented:

object Stuff {
  def apply(i: Int): Stuff = new Stuff(i)
}

So there's no harm at all in using new Stuff.

尐籹人 2024-10-01 12:27:41

对于问题

正在使用构造函数彻底实例化案例类

答案肯定是肯定的。类似的事情

val b = new Stuff(1)

根本不会造成任何问题。现在,new Stuff(1) with Marker 有所不同,因为创建了 Stuff 的匿名子类。不过,我相信这仍然没有问题。当案例类从其他案例类继承时,我所知道的问题就会出现,而您却没有这样做。但我可能不知道一些事情。

编辑:刚刚在 REPL 中测试了匹配:

scala> val b = new Stuff(1)
b: Stuff = Stuff(1)

scala> b match {case Stuff(x) => x}
res0: Int = 1

scala> b match {case Stuff(_) => true}
res1: Boolean = true

To the question

is instantiating case classes using a constructor kosher

the answer is definitely yes. Something like

val b = new Stuff(1)

poses no problems at all. Now, new Stuff(1) with Marker is different because an anonymous subclass of Stuff is created. I believe, though, that it's still unproblematic. The problems I know about appear when case classes inherit from other case classes, and you aren't doing that. But I may be unaware of something.

EDIT: just tested matching in the REPL:

scala> val b = new Stuff(1)
b: Stuff = Stuff(1)

scala> b match {case Stuff(x) => x}
res0: Int = 1

scala> b match {case Stuff(_) => true}
res1: Boolean = true
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文