如何测试案例类构造函数的参数?

发布于 2024-09-18 06:46:13 字数 159 浏览 4 评论 0原文

我想测试我的案例类构造函数的参数,如果它们未通过某些测试,则抛出异常。当我尝试编写自己的 apply 方法(多个“apply”方法)时,编译器抱怨。

我想我可以将其设为非 case 类,并自己执行 apply/unapply 构造函数字段的内容,但我希望不要这样做。

谢谢

I'd like to test the arguments to my case class constructor and throw an exception if they fail certain tests. The compiler complained when I tried to write my own apply method (Multiple 'apply' methods.

I suppose I could make it a non-case class, and do the apply/unapply constructor field stuff myself, but I had hoped not to.

Thanks

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

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

发布评论

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

评论(1

苍白女子 2024-09-25 06:46:16
case class Picky(i: Int, s: String) {
  require(i % 2 == 0, "i must be even")
  require(s.length < 50, "s length must be less than 50 characters")

  // ...
}
case class Picky(i: Int, s: String) {
  require(i % 2 == 0, "i must be even")
  require(s.length < 50, "s length must be less than 50 characters")

  // ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文