fork download
  1. object Main extends App {
  2. object MySingleton {
  3. def foo = 19
  4. }
  5.  
  6. val a = MySingleton
  7. val b = MySingleton
  8.  
  9. println(a == b) //object equality
  10. println(a eq b) //ref equality
  11.  
  12. println(MySingleton.foo)
  13. println(a.foo)
  14. println(b.foo)
  15.  
  16. val c:MySingleton.type = MySingleton
  17.  
  18. class Employee(private val eid:String, val firstName:String, val lastName:String) extends AnyRef {
  19.  
  20. require (eid.nonEmpty, "EID cannot be blank") //Precondition, throw IllegalArgumentException
  21. require (firstName.nonEmpty, "firstName cannot be blank")
  22. require (lastName.nonEmpty, "lastName cannot be blank")
  23.  
  24. def this(firstName:String, lastName:String) = this("000-00-0000", firstName, lastName)
  25.  
  26. def this(firstName:String) = this(firstName, "Unknown")
  27.  
  28. def this() = this("Unknown")
  29.  
  30. override def toString = s"Employee($firstName, $lastName)"
  31.  
  32. }
  33.  
  34.  
  35. class Manager(firstName:String, lastName:String, val employee:List[Employee])
  36. extends Employee(firstName, lastName)
  37.  
  38.  
  39. object Employee {
  40. def create3WithSameName(firstName:String, lastName:String):List[Employee] = {
  41. List(new Employee(firstName, lastName),
  42. new Employee(firstName, lastName),
  43. new Employee(firstName, lastName))
  44. }
  45.  
  46. def retrieveSecretInfo(x:Employee) = {
  47. x.eid
  48. }
  49. }
  50.  
  51. println(Employee.create3WithSameName("Diane", "Sawyer"))
  52.  
  53. println(Int.MinValue)
  54.  
  55. val d = new Employee("100-00-1029", "Laura", "Bocelli")
  56.  
  57. //println(d.eid) //doesn't work
  58.  
  59. println(Employee.retrieveSecretInfo(d))
  60.  
  61. class Foo(x:Int) {
  62.  
  63. def apply(y:Int) = x + y
  64. }
  65.  
  66. val foo = new Foo(18)
  67. println(foo.apply(30))
  68. println(foo(30))
  69.  
  70. val list = List(1,2,3,4,5,6)
  71. val list2 = List.apply(1,2,3,4,5,6) //If you don't see a new, look in the companion object
  72.  
  73. val item2 = list(1)
  74. val item2alternate = list.apply(1)
  75.  
  76.  
  77.  
  78.  
  79. }
Success #stdin #stdout 0.39s 323264KB
stdin
Standard input is empty
stdout
true
true
19
19
19
List(Employee(Diane, Sawyer), Employee(Diane, Sawyer), Employee(Diane, Sawyer))
-2147483648
100-00-1029
48
48