Skip to content

Instantly share code, notes, and snippets.

@archena
Created October 8, 2014 11:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save archena/15d6825fbad8ba701b57 to your computer and use it in GitHub Desktop.
Save archena/15d6825fbad8ba701b57 to your computer and use it in GitHub Desktop.
new T
/**
* 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