I want to learn scala language.
In many documents or video tutorials, I see scala developers create empty class or objects and use that on another class as parameter or implement empty traits !
for example :
object Controller {
sealed trait Controller
case object Login extends Controller
case object Logout extends Controller
}
Or this :
sealed trait Expression
case class Number(num: Int) extends Expression
case class Plus(a: Expression, b: Expression) extends Expression
case class Minus(a: Expression, b: Expression) extends Expression
object ExpressionEvaluate {
def value(expression: Expression): Int = expression match {
case Number(value) => value
case Plus(a, b) => value(a) + value(b)
case Minus(a, b) => value(a) - value(b)
}
}
I want to know what is this pattern ?
What is meaning empty class ,Object or traits ?
Why developers use this pattern ?
This pattern:
sealed trait Controller
case object Login extends Controller
case object Logout extends Controller
Or this
sealed trait Expression
case class Number(num: Int) extends Expression
case class Plus(a: Expression, b: Expression) extends Expression
case class Minus(a: Expression, b: Expression) extends Expression
Are known as algebraic data types . They are the Scala way of creating a co-product/sum types (also known as a tagged union). When you receive a trait of type Expression
in your signature, you know that you will receive one of the concrete implementations. The way you discover which type is the concrete type is via pattern matching , which is the ability to match on types. More so, the compiler is smart enough with a sealed trait
to know all its underlying concrete types and let you know in case your pattern match isn't exhaustive, for example, if I add a Divide
type:
case class Divide(a: Expression, b: Expression) extends Expression
And compile, the compiler will complain:
Warning:(18, 48) match may not be exhaustive.
It would fail on the following input: Divide(_, _)
def value(expression: Expression): Int = expression match {
As for the "empty classes", we have to distinguish between two types. One is a case class
. A case class is a way of creating immutable record types in Scala, think of them as a product type on steroids. These are classes in which the compiler automatically derives an implementation of hashCode
and equals
, and also adds methods which provide us syntax sugar when we pattern match via apply
/ unapply
. For example:
case Number(value) => value
You see that we are able to access the value
field on Number
in the pattern match, how is that possible? It is possible because the compiler provides us an unapply
method which is used to deconstruct the case class.
The other one is a case object
, which is a way of creating a singleton type (a type which represents exactly one value) in Scala. Since we don't have any fields on Login
or Logout
, we can create a single representation of that type.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.