There are safer alternatives for altering case classes:
Case classes can be disassembled to their constituent parts as a tuple:
Scala supports the notion of _case classes_.
Scala supports the notion of _case classes_. Case classes are regular classes which export their constructor parameters and which provide a recursive decomposition mechanism via pattern matching.
Here is an example for a class hierarchy which consists of an abstract super class Term
and three concrete case classes Var
, Fun
, and App
.
abstract class Term case class Var(name: String) extends Term case class Fun(arg: String, body: Term) extends Term case class App(f: Term, v: Term) extends Term
This class hierarchy can be used to represent terms of the untyped lambda calculus. To facilitate the construction of case class instances, Scala does not require that the new
primitive is used. One can simply use the class name as a function.
Here is an example:
Fun("x", Fun("y", App(Var("x"), Var("y"))))
The constructor parameters of case classes are treated as public values and can be accessed directly.
val x = Var("x") Console.println(x.name)
For every case class the Scala compiler generates equals
method which implements structural equality and atoString
method. For instance:
val x1 = Var("x") val x2 = Var("x") val y1 = Var("y") println("" + x1 + " == " + x2 + " => " + (x1 == x2)) println("" + x1 + " == " + y1 + " => " + (x1 == y1))
will print
Var(x) == Var(x) => true Var(x) == Var(y) => false
It only makes sense to define case classes if pattern matching is used to decompose data structures. The following object defines a pretty printer function for our lambda calculus representation:
object TermTest extends Application { def printTerm(term: Term) { term match { case Var(n) => print(n) case Fun(x, b) => print("^" + x + ".") printTerm(b) case App(f, v) => Console.print("(") printTerm(f) print(" ") printTerm(v) print(")") } } def isIdentityFun(term: Term): Boolean = term match { case Fun(x, Var(y)) if x == y => true case _ => false } val id = Fun("x", Var("x")) val t = Fun("x", Fun("y", App(Var("x"), Var("y")))) printTerm(t) println println(isIdentityFun(id)) println(isIdentityFun(t)) }
In our example, the function print
is expressed as a pattern matching statement starting with the match
keyword and consisting of sequences of case Pattern => Body
clauses.
The program above also defines a function isIdentityFun
which checks if a given term corresponds to a simple identity function. This example uses deep patterns and guards. After matching a pattern with a given value, the guard (defined after the keyword if
) is evaluated. If it returns true
, the match succeeds; otherwise, it fails and the next pattern will be tried.
Case classes have an automatic equals method that works:
Case classes can be created in a convenient way:
Case classes have an automatic hashcode method that works:
Case classes can have mutable properties:
Case classes can have default and named parameters:
Case classes have automatic properties:
Case classes are Serializable
toStringMethodCaseClasses
toStringMethodCaseClasses
Case classes have a convenient toString method defined:
This method has been deprecated in favor of macro assertion and will be removed in a future version of ScalaTest. If you need this, please copy the source code into your own trait instead.
This method has been deprecated in favor of macro assertion and will be removed in a future version of ScalaTest. If you need this, please copy the source code into your own trait instead.
This method has been deprecated in favor of macro assumption and will be removed in a future version of ScalaTest. If you need this, please copy the source code into your own trait instead.
This method has been deprecated in favor of macro assumption and will be removed in a future version of ScalaTest. If you need this, please copy the source code into your own trait instead.
Please use 'an [Exception] should be thrownBy { ... }' syntax instead
This expect method has been deprecated. Please replace all invocations of expect with an identical invocation of assertResult instead.
This expect method has been deprecated. Please replace all invocations of expect with an identical invocation of assertResult instead.
This expectResult method has been deprecated. Please replace all invocations of expectResult with an identical invocation of assertResult instead.
This expectResult method has been deprecated. Please replace all invocations of expectResult with an identical invocation of assertResult instead.