Created
January 31, 2015 18:03
-
-
Save na2hiro/f62246100f7bfece8815 to your computer and use it in GitHub Desktop.
Javaにおいてダウンキャストの代替となるパターンマッチ構文の一案
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
Number number = getNumber(); | |
// before | |
if(number instanceof Integer){ | |
((Integer)number).someIntegerMethod(); | |
}else if(number instanceof Float){ | |
((Float)number).someFloatMethod(); | |
} | |
// after | |
match(number){ | |
case Integer: | |
number.someIntegerMethod(); | |
case Float: | |
number.someFloatMethod(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment