在关于RegexParsers 的Scaladoc 中,有以下代码:
object Calculator extends RegexParsers {
def number: Parser[Double] = """\d+(\.\d*)?""".r ^^ { _.toDouble }
def factor: Parser[Double] = number | "(" ~> expr <~ ")"
...
我不明白为什么我们用 adef
而不是 a val
or a来写它lazy val
?我会这样写:
object Calculator extends RegexParsers {
lazy val number: Parser[Double] = """\d+(\.\d*)?""".r ^^ { _.toDouble }
lazy val factor: Parser[Double] = number | "(" ~> expr <~ ")"
...