Skip to content

Instantly share code, notes, and snippets.

@xfire
Created June 6, 2013 06:58
Show Gist options
  • Save xfire/5719787 to your computer and use it in GitHub Desktop.
Save xfire/5719787 to your computer and use it in GitHub Desktop.
Joint union in type parameter variance.
class Test {
interface Foo {
int doFoo();
}
interface Bar {
int doBar();
}
// here we use the union type parameter: Foo & Bar
static class TypeUnion<T extends Foo & Bar> {
public int test(final T thingi) {
return thingi.doFoo() + thingi.doBar();
}
}
static class Thingi implements Foo, Bar {
public int doFoo() { return 23; }
public int doBar() { return 42; }
}
public static void main(final String args[]) {
final TypeUnion<Thingi> tu = new TypeUnion<Thingi>();
final Thingi thingi = new Thingi();
System.out.println(" foo: " + thingi.doFoo());
System.out.println(" bar: " + thingi.doBar());
System.out.println("foo + bar: " + tu.test(thingi));
}
}
@xfire
Copy link
Author

xfire commented Jun 6, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment