Last active
December 17, 2015 14:49
-
-
Save takezoux2/5627080 to your computer and use it in GitHub Desktop.
scala勉強会で出た話題のコード。
sealed classを使ってEnumerationと同じようなことを行う。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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