Skip to content

Instantly share code, notes, and snippets.

@rupeshtr78
Last active November 4, 2020 02:12
Show Gist options
  • Save rupeshtr78/0e0a33d1d1dbc3eb4ac0a831a4affd25 to your computer and use it in GitHub Desktop.
Save rupeshtr78/0e0a33d1d1dbc3eb4ac0a831a4affd25 to your computer and use it in GitHub Desktop.
case class
case class Person(name: String, age: Integer, favColor: String)
case class Person(name:String,age:Int = 0)
val p1 = Person("Rupesh",42)
val p2 = Person("Roopa")
case class Person(name:String,age:Option[Int] = Some(0))
val p1 = Person("Rupesh",Some(42))
val p2 = Person("Roopa")
println(p1)
println(p2)
//Person(Rupesh,Some(42))
//Person(Roopa,Some(0))
case class PersonRequire(
firstName:String,
lastName:String,
age:Int){
require(firstName != "")
require(lastName != "")
}
try {
val pr1 = PersonRequire("", "Raghavan", 42)
println(pr1)
} catch {
case e:Throwable => println(e.getMessage) // requirement failed
e.printStackTrace()
}
//java.lang.IllegalArgumentException: requirement failed
case class Person(name:String,age:Option[Int] = Some(0))
val p1 = Person("Rupesh",Some(42))
val p2 = Person("Roopa")
val p3 = Person("Rhea",Some(5))
val p4 = Person("Reva",Some(3))
case class Friends(person:Person , friends:List[Person])
val friends1 = Friends(p1,List(p2,p3,p4))
val friends2 = friends1.copy()
val friends3 = friends2.copy(person = p2)
println(friends3)
//Friends(Person(Rupesh,Some(42)),List(Person(Roopa,Some(0)), Person(Rhea,Some(5)), Person(Reva,Some(3))))
//Friends(Person(Rupesh,Some(42)),List(Person(Roopa,Some(0)), Person(Rhea,Some(5)), Person(Reva,Some(3))))
//Friends(Person(Roopa,Some(0)),List(Person(Roopa,Some(0)), Person(Rhea,Some(5)), Person(Reva,Some(3))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment