| 1. |
What Is Apply Method In Scala? What Is Unapply Method In Scala? What Is The Difference Between Apply And Unapply Methods In Scala? |
|
Answer» In Scala, apply and unapply methods play very important ROLE. They are also very useful in Play Framework in mapping and unmapping data between Form data and MODEL data. In simple words, apply method: To compose or assemble an object from it’s components. unapply method: To decompose or dis-assemble an object into it’s components. Scala’s apply method: It is used to compose an object by using its components. Suppose if we WANT to create a Person object, then use firstName and laststName two components and compose Person object as shown below. class Person(val firstName: String, val lastName: String) Scala’s unapply method: It is used to decompose an object into its components. It FOLLOWS reverse process of apply method. Suppose if we have a Person object, then we can decompose this object into it’s two components: firstName and laststName as shown below. class Person(val firstName: String, val lastName: String) In Scala, apply and unapply methods play very important role. They are also very useful in Play Framework in mapping and unmapping data between Form data and Model data. In simple words, apply method: To compose or assemble an object from it’s components. unapply method: To decompose or dis-assemble an object into it’s components. Scala’s apply method: It is used to compose an object by using its components. Suppose if we want to create a Person object, then use firstName and laststName two components and compose Person object as shown below. class Person(val firstName: String, val lastName: String) Scala’s unapply method: It is used to decompose an object into its components. It follows reverse process of apply method. Suppose if we have a Person object, then we can decompose this object into it’s two components: firstName and laststName as shown below. class Person(val firstName: String, val lastName: String) |
|