Last active
December 20, 2015 22:58
-
-
Save vaskoz/6208429 to your computer and use it in GitHub Desktop.
Function Interface & Descriptor Examples from JLS 8 - JSR 335
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
// Function descriptor examples: | |
interface X { void m() throws IOException; } | |
interface Y { void m() throws EOFException; } | |
interface Z { void m() throws ClassNotFoundException; } | |
interface XY extends X, Y {} | |
interface XYZ extends X, Y, Z {} | |
// XY has descriptor ()->void throws EOFException | |
// XYZ has descriptor ()->void (throws nothing) | |
interface A { | |
List<String> foo(List<String> arg) throws IOException, SQLTransientException; | |
} | |
interface B { | |
List foo(List<String> arg) throws EOFException, SQLException, TimeoutException; | |
} | |
interface C { | |
List foo(List arg) throws Exception; | |
} | |
interface D extends A, B {} | |
interface E extends A, B, C {} | |
// D has descriptor (List<String>)->List<String> throws EOFException, SQLTransientException | |
// E has descriptor (List)->List throws EOFException, SQLTransientException | |
interface G1 { | |
<E extends Exception> Object m() throws E; | |
} | |
interface G2 { | |
<F extends Exception> String m() throws Exception; | |
} | |
interface G extends G1, G2 {} | |
// G has descriptor <F extends Exception> ()->String throws F |
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
// Functional interface examples: | |
interface Runnable { void run(); } | |
// Functional | |
interface Foo { boolean equals(Object obj); } | |
// Not functional; equals is already an implicit member | |
interface Bar extends Foo { int compare(String o1, String o2); } | |
// Functional; Bar has one abstract non-Object method | |
interface Comparator<T> { | |
boolean equals(Object obj); | |
int compare(T o1, T o2); | |
} | |
// Functional; Comparator has one abstract non-Object method | |
interface Foo { | |
int m(); | |
Object clone(); | |
} | |
// Not functional; method Object.clone is not public | |
interface X { int m(Iterable<String> arg); } | |
interface Y { int m(Iterable<String> arg); } | |
interface Z extends X, Y {} | |
// Functional: two methods, but they have the same signature | |
interface X { Iterable m(Iterable<String> arg); } | |
interface Y { Iterable<String> m(Iterable arg); } | |
interface Z extends X, Y {} | |
// Functional: Y.m is a subsignature & return-type-substitutable | |
interface X { int m(Iterable<String> arg); } | |
interface Y { int m(Iterable<Integer> arg); } | |
interface Z extends X, Y {} | |
// Not functional: No method has a subsignature of all abstract methods | |
interface X { int m(Iterable<String> arg, Class c); } | |
interface Y { int m(Iterable arg, Class<?> c); } | |
interface Z extends X, Y {} | |
// Not functional: No method has a subsignature of all abstract methods | |
interface X { long m(); } | |
interface Y { int m(); } | |
interface Z extends X, Y {} | |
// Compiler error: no method is return type substitutable | |
interface Foo<T> { void m(T arg); } | |
interface Bar<T> { void m(T arg); } | |
interface FooBar<X, Y> extends Foo<X>, Bar<Y> {} | |
// Compiler error: different signatures, same erasure | |
interface Foo<T, N extends Number> { | |
void m(T arg); | |
void m(N arg); | |
} | |
interface Bar extends Foo<String, Integer> {} | |
interface Baz extends Foo<Integer, Integer> {} | |
// Foo is _not_ functional: different signatures for m | |
// Bar is _not_ functional: different signatures for m | |
// Baz is functional: same signature for m | |
interface Executor { <T> T execute(Action<T> a); } | |
// Functional | |
interface X { <T> T execute(Action<T> a); } | |
interface Y { <S> S execute(Action<S> a); } | |
interface Exec extends X, Y {} | |
// Functional: signatures are "the same" | |
interface X { <T> T execute(Action<T> a); } | |
interface Y { <S,T> S execute(Action<S> a); } | |
interface Exec extends X, Y {} | |
// Compiler error: different signatures, same erasure |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment