Created
October 8, 2014 11:22
-
-
Save archena/15d6825fbad8ba701b57 to your computer and use it in GitHub Desktop.
new T
This file contains 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
/** | |
* We can't write 'new T()' in Java, and it wouldn't be typesafe if we could. Some people blame type erasure for this, but | |
* the real problem is that we haven't specified that T has a zero-argument constructor, we just assumed it. | |
* To do it properly, encode the constructors through generics. No runtime type information needed... | |
*/ | |
interface Constructor0<T> { | |
T make(); | |
} | |
class Example0<T, F extends Constructor0<T>> { | |
private final T thing; | |
public Example0(F factory) { | |
this.thing = factory.make(); | |
} | |
} | |
interface Constructor1<T, A> { | |
T make(A a); | |
} | |
class Example1<T, F extends Constructor1<T, Integer>> { | |
private final T thing; | |
public Example1(F factory) { | |
this.thing = factory.make(12); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment