Created
November 26, 2012 12:33
-
-
Save aoiroaoino/4147981 to your computer and use it in GitHub Desktop.
match式にてcase classのチェック漏れを検出 ref: http://qiita.com/items/30d02a5306514df67de0
This file contains 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
sealed abstract class Season | |
case class Spring() extends Season | |
case class Summer() extends Season | |
case class Fall() extends Season | |
case class Winter() extends Season | |
// FallとWinterを忘れたパターンマッチ | |
scala> def checkSeason(s: Season) = s match { | |
| case Spring() => "Spring!" | |
| case Summer() => "Summer!" | |
| } |
This file contains 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
<console>:12: warning: match is not exhaustive! | |
missing combination Fall | |
missing combination Winter | |
def checkSeason(s: Season) = s match { | |
^ | |
checkSeason: (s: Season)java.lang.String |
This file contains 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
scala> def checkSeason(s: Season) = (s: @unchecked) match { | |
| case Spring() => "Spring!" | |
| case Summer() => "Summer!" | |
| } |
This file contains 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
scala> checkSeason(Fall()) | |
scala.MatchError: Fall() (of class Fall) | |
at .checkSeason(<console>:12) | |
(以下省略) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment