Skip to content

Instantly share code, notes, and snippets.

@akiomik
Last active January 3, 2016 07:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akiomik/8430975 to your computer and use it in GitHub Desktop.
Save akiomik/8430975 to your computer and use it in GitHub Desktop.
case class and companion object
case class Calc(f: Int => Int, g: Int => Int, i: Int = 1) {
  def calc: Int = (f compose g)(i)
}

object Calc {
  def apply(f: Int => Int, g: Int => Int): Calc = new Calc(f, g)
}

// case class
val c1 = new Calc(_ * 10, _ + 10)
c1.calc // => 110

// case class
val c2 = Calc(_ * 10, _ + 10, 1)
c2.calc // => 110

// companion object
val c3 = Calc(_ * 10, _ + 10)
// <console>:12: error: missing parameter type for expanded function ((x$1) => x$1.$times(10))
//              Calc(_ * 10, _ + 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment