简体   繁体   中英

Understanding of Case class in Scala

I have below code but when i remove case statement , object instance(Donut) cannot able to refer . I want to understand with simple example with class and case class statements , Please advise . Also i want to understand why '(' ')' is printed in my second print statement.

 case class Donut(name: String, tasteLevel: String)

   val favoriteDonut2: Donut = Donut("Glazed Donut", "Very Tasty")
   println(s"My favorite donut name = ${favoriteDonut2.name}, 
          tasteLevel =  ${favoriteDonut2.tasteLevel}")
   println( s"My fav donut name is = ${favoriteDonut2.name}",
            s"taste level is       = ${favoriteDonut2.tasteLevel}")

output:-
   My favorite donut name = Glazed Donut, tasteLevel = Very Tasty
   (My fav donut name is = Glazed Donut,taste level is     = Very Tasty)

When you call Donut("Glazed Donut", "Very Tasty") you're really calling the apply method of the Donut object .
When you mark a class as a case class , scala will automatically generate a bunch of things for you, between them a companion object for your class with an apply method that takes all the arguments you define.
That's why if you define it as just a normal class, the line will fail - you can call new Donut("Glazed Donut", "Very Tasty") instead. Or you could create the apply method by hand.

class Donut(val name: String, val tasteLevel: String) // vals needed to make the fields public, case classes do this by default.
object Donut {
  /** Factory of Donuts */
  def apply(name: String, tasteLevel: String): Donut = new Donut(name, tasteLevel)
}

Note

Case Classes provide another bunch of useful functionalities appart of a simple constructor, like: pretty toString implementation, good hashCode implementation, "by value" comparison, simple "copying" , extractors for pattern matching , etc - that remove "boilerplate" from the code.
Thus if you need a couple of these features and the class is not intended to be mutated, prefer a case class than implementing everything by yourself.

" Also i want to understand why '(' ')' is printed in my second print statement."

Scala's print function, only receives one argument. Thus, what happens when you write print(a, b) is that you call print with the tuple (a, b) - (which is the same as calling print((a, b)) ) .

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM