Fahad/CodeSamples/ExtractorObjects.scala
author Chengsong
Mon, 09 May 2022 17:20:55 +0100
changeset 512 a4b86ced5c32
parent 44 a751aa1ee4f7
permissions -rw-r--r--
rewrite rules modified slightly

package Main

object Twice {
  def apply(x: Int): Int = x * 2
  def unapply(z: Int): Option[Int] = if (z % 2 == 0) Some(z / 2) else None
}

object TwiceTest extends App {
  val x = Twice(10)
  x match { case Twice(n) => Console.println(n) } //prints 21
}