Fahad/CodeSamples/ExtractorObjects.scala
author Chengsong
Sun, 09 Jul 2023 00:29:02 +0100
changeset 657 00171b627b8d
parent 44 a751aa1ee4f7
permissions -rw-r--r--
Fixed some annotated/unannotated a/r notation inconsistencies.

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
}