Difference between case class and case object?

后端 未结 4 452
一整个雨季
一整个雨季 2021-01-30 06:53

I am learning Scala and Akka and in my recent lookup for a solution, I found something like

 case class TotalTaxResult(taxAmount:Double)
 case object TaxCalcul         


        
4条回答
  •  日久生厌
    2021-01-30 07:07

    A case object is a singleton case class. They are used kind of like enumeration values. It can be used in pattern matching just like any other value:

    TaxCalculationTimeout match {
      case TaxCalculationTimeout => println("hello")
    }
    

    When you define a case class, you are creating a template for instances of that class. TotalTaxResult(1.0) and TotalTaxResult(2.0) are two different values of the same type. Whereas there is exactly one TaxCalculationTimeout value.

提交回复
热议问题