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
| class PartitionFoos { | |
| void doStuff(Collection<Foo> foos) { | |
| final List<Bar> bars = new ArrayList<>(); | |
| final List<Baz> bazes = new ArrayList<>(); | |
| for (Foo f : foos) { | |
| f.match(new Visitor<Void>() { | |
| @Override | |
| public Void caseBar(Bar b) { | |
| bars.add(b); |
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
| class DefaultFooVisitor<T> implements Foo.Visitor<T> { | |
| T defaultValue; | |
| private DefaultFooVisitor(T defaultValue) { | |
| this.defaultValue = defaultValue; | |
| } | |
| @Override | |
| public T caseBar(Bar b) { | |
| return defaultValue; |
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
| abstract class Foo { | |
| abstract <T> T match(Visitor<T> visitor); | |
| interface Visitor<T> { | |
| T caseBar(Bar b); | |
| T caseBaz(Baz b); | |
| } | |
| static class Bar extends Foo { | |
| final int bar; |
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
| abstract class OoFoo { | |
| abstract void handle(); | |
| static class Bar extends OoFoo { | |
| private int bar; | |
| private Bar(int bar) { | |
| this.bar = bar; | |
| } |
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
| abstract class BadFoo { | |
| static class Bar extends BadFoo { | |
| int bar; | |
| private Bar(int bar) { | |
| this.bar = bar; | |
| } | |
| } |
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
| abstract class Foo { | |
| abstract <T> T match(Visitor<T> visitor); | |
| interface Visitor<T> { | |
| T caseManyBar(); | |
| T caseBar(int i); | |
| T caseBaz(String s); | |
| } | |
| static class Bar extends Foo { |
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
| enum Foo { | |
| case Bar(bar: Int) | |
| case Baz(baz: String) | |
| } | |
| func handle(f: Foo) -> Any { | |
| switch f { | |
| case .Bar(let bar): | |
| return bar | |
| case .Baz(let baz): |
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 patterns { | |
| abstract class Foo | |
| case class Bar(val bar: Int) extends Foo | |
| case class Baz(val baz: String) extends Foo | |
| def handle(f: Foo) = | |
| f match { | |
| case Bar(i) if (i > 10) => 10 | |
| case Bar(i) => i | |
| case Baz(s) => s |
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 patterns { | |
| abstract class Foo | |
| class Bar(val bar: Int) extends Foo | |
| class Baz(val baz: String) extends Foo | |
| def handle(f: Foo) = | |
| f match { | |
| case b: Bar => b.bar | |
| case b: Baz => b.baz | |
| } |
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
| class Account { | |
| private Strategy strategy; | |
| private Map<String, Double> investments; | |
| boolean addInvestment(String symbol, double fraction, double riskiness) { | |
| if (strategy.isRiskAcceptable(fraction, riskiness)) { | |
| investments.put(symbol, fraction); | |
| return true; | |
| } | |
| return false; |
NewerOlder