【发布时间】:2015-11-01 15:42:13
【问题描述】:
我正在尝试创建一个具有多个构造函数的案例类:
object App {
def main(args: Array[String]) {
val a = Something("abc", 100500, _ % 2 == 0)
val b = Something(true, 10, 20)
println(s"$a - $b")
}
}
case class Something(s: String, n: Int, p: Int => Boolean) {
/*
Additional constructor -- Wrong way! -- it is imposible to invoke it outside the class
def this(b: Boolean, x: Int, y: Int) {
this("", 0, (i: Int) => i % x + y == 0)
}
*/
}
到目前为止,我的代码不起作用:
Error:(10, 23) type mismatch;
found : Boolean(true)
required: String
val b = Something(true, 10, 20)
^
要修复它,我需要创建一个伴随对象来保存一个应用函数,该函数代表Something 类的新构造函数:
object Something {
def apply(b: Boolean, x: Int, y: Int) = {
new Something(if (b) "" else "default", 0, _ => {
(x + y) % 2 == 0
})
}
}
不方便。也许还有其他方法可以将多个构造函数放入案例类?
【问题讨论】:
-
我认为伴随对象是唯一的方法,因为案例类是代数类型,它不应该有不同的形式(不同的构造函数)。如果你可以为它创建辅助构造函数——我认为它不能很好地与所有案例类功能一起使用——比如模式匹配
-
您提到了代数类型。于是我查了一下wiki是什么意思,发现是这样的:
Each variant has its own constructor, which takes a specified number of arguments with specified types.所以代数类型允许多个构造函数。 -
我认为不相交的意思是:
abstract class Base; case class A(i: Int) extends Base; case class B(s: String) extends Base现在您可以使用Integer或String的类型,这是Base的两个构造函数。但它看起来并不能解决你的问题。 -
Martin O. 谈辅助构造函数here
-
是的,我知道。我以前读过。但我的问题是关于案例类的。
标签: scala