logo       

pattern matching works strange for an O'Caml programmer: msg#00241

lang.scala

Subject: pattern matching works strange for an O'Caml programmer

Hello.

At first, I had this:
abstract class Expr {
def eval: int = this match {
case Number(n) => n
case Sum(e1, e2) => e1.eval + e2.eval
}
}
case class Number(n: int) extends Expr
case class Sum(e1: Expr, e2: Expr) extends Expr

It works great and makes perfect sense to me.

Later, I made it into this:
abstract class Expr {
def eval: int = this match {
case Number(n) => n
//case Sum(e1, e2) => e1.eval + e2.eval
}
}
case class Number(n: int) extends Expr
case class Sum(e1: Expr, e2: Expr) extends Expr

The warning I expected to receive didn't show up, that was
first thing that upset me.

Even later, I made it into this:
abstract class Expr {
def eval: int = this match {
case Number(n) => n
case Sum(x, 0) => 0
}
}
case class Number(n: int) extends Expr
case class Sum(e1: Expr, e2: Expr) extends Expr

The type error didn't showed up either.

These two errors are one of the most common mistakes that
I make during development, that's why I do love O'Caml.

Is there possibility that those errors/warnings will be
implemented in Scala? I really need them.

--
WBR, Alexander Sergeev.




<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise