Skip to content

Instantly share code, notes, and snippets.

@takezoux2
Last active December 17, 2015 14:49
Show Gist options
  • Save takezoux2/5627080 to your computer and use it in GitHub Desktop.
Save takezoux2/5627080 to your computer and use it in GitHub Desktop.
scala勉強会で出た話題のコード。 sealed classを使ってEnumerationと同じようなことを行う。
object App
{
def main(args : Array[String]){
doMatch(args[0].toInt)
}
def doMatch( id : Int) = {
id match{
// scala2.10では()つけないとコンパイルエラーなのであまりいけてない?
case Status.OK() => println("OK")
case Status.Error() => prinltn("Error")
}
}
}
sealed abstract class Status(id : Int){
def unapply( id : Int) : Option[Status] = {
if(this.id == id) Some(this)
else None
}
}
object Status{
case object OK extends Status(1)
case object Error extends Status(2)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment