2장 코드 연동


Netbean를 이용한 Creating JPA entities and REST web services 구축은 skip...

Adding a test in Scala

Add 'scalatest' library in build.sbt

libraryDependencies += "org.scalatest" % "scalatest_2.10" % "2.0" % "test"

Trait

  • Trait은 다른 클래스가 확장 or 상속 하거나 섞어 넣을 수 있는(Mix in) field와 method 모음
  • 클래스는 여러 트레잇를 with 키워드를 사용해 확장할 수 있다.
trait Car {
  val brand: String
}

trait Shiny {
  val shineRefraction: Int
}
class BMW extends Car {
  val brand = "BMW"
}

class BMW extends Car with Shiny {
  val brand = "BMW"
  val shineRefraction = 12
}

Factory Method

  • Factory method case 1 : Case class
case class Customer(val name:String)
  • Factory method case 2 : Companion objects
object Customer {
   def apply()= new Customer("default name")
}
class Customer(name:String) {
 ...
}

Exception

  • catch문 안에서의 return value에 주의
  • Either, Failure, Success 등의 객체를 이용해 처리

Code layout & Naming conventions

  • camel case
  • constants - upper camel case
object Constants {
    val MyNeverChangingAge = 20 //constants
}

Sample Source