Skip to content

Instantly share code, notes, and snippets.

@na2hiro
Created January 31, 2015 18:03
Show Gist options
  • Save na2hiro/f62246100f7bfece8815 to your computer and use it in GitHub Desktop.
Save na2hiro/f62246100f7bfece8815 to your computer and use it in GitHub Desktop.
Javaにおいてダウンキャストの代替となるパターンマッチ構文の一案
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