这是Scala 构造函数重载的后续问题?我想要一个案例类构造函数,它在输入的受限形式上运行,因此覆盖,而不是重载构造函数:
abstract class Expr
case class RegExpr(regex : Regex) extends Expr {
override def this(regex : Regex) = {
if (regex.toString contains "*")
throw new Exception("Restricted class of regular expressions: cannot contain Kleene star.")
else if (regex.toString contains "|")
throw new Exception("Restricted class of regular expressions: cannot contain disjunction.")
else this(regex)
}
}
这不编译;我已经尝试了几个不同的迭代,但它们都回到编译器告诉我它需要'this',但是找到了'if'。如何获得我想要的行为?