fork(1) download
  1. object Main extends App {
  2. println("Actually executing")
  3. // val studentList = Option(Seq(
  4. // new Student("alex", "seattle", "92394"),
  5. // new Student("james", "london", "32e12321"),
  6. // new Student("jon", "denvor", "123123")
  7. // ))
  8.  
  9. // val studentList = Option(Seq.empty[Student])
  10.  
  11. val studentList = None
  12.  
  13. val studentFull = Seq(
  14. new Student("alex", "seattle", "92394"),
  15. new Student("james", "london", "32e12321"),
  16. new Student("jon", "denvor", "123123")
  17. )
  18.  
  19. val studentEmpty = Seq.empty[Student]
  20.  
  21. println("Student full " + studentFull.headOption)
  22. println("Student empty " + studentEmpty.headOption)
  23.  
  24. val maybePerson = studentFull.headOption match {
  25. case Some(p) => Option(new Person(
  26. p.name, p.address))
  27. case _ => None
  28. }
  29.  
  30. println("Maybe person from student full " + maybePerson.toString)
  31.  
  32. val maybePerson2 = studentEmpty.headOption match {
  33. case Some(p) => Some(new Person(
  34. p.name, p.address))
  35. case _ => None
  36. }
  37.  
  38. println("Maybe person from student empty " + maybePerson2.toString)
  39.  
  40. val maybePerson3 = studentList.getOrElse(Seq.empty).headOption match {
  41. case Some(p) => {
  42. println("case 1")
  43. Some(new Person(
  44. "abc", "bcd"))
  45. }
  46. case _ => {
  47. println("case 2")
  48. None
  49. }
  50. }
  51.  
  52. println("Maybe person from student " + maybePerson3.toString)
  53. }
  54.  
  55. case class Student (
  56. name: String,
  57. address: String,
  58. phoneNo: String
  59. )
  60.  
  61. case class Person (
  62. name: String,
  63. address: String
  64. )
Success #stdin #stdout 0.47s 67432KB
stdin
Standard input is empty
stdout
Actually executing
Student full Some(Student(alex,seattle,92394))
Student empty None
Maybe person from student full Some(Person(alex,seattle))
Maybe person from student empty None
case 2
Maybe person from student None