Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Java default visitor
class DefaultFooVisitor<T> implements Foo.Visitor<T> {
T defaultValue;
private DefaultFooVisitor(T defaultValue) {
this.defaultValue = defaultValue;
}
@Override
public T caseBar(Bar b) {
return defaultValue;
}
@Override
public T caseBaz(Baz b) {
return defaultValue;
}
static int countBars(Foo f) {
return f.match(new DefaultFooVisitor<Integer>(0) {
@Override
public Integer caseBar(Bar b) {
return b.bar;
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment