My colleague Rod presented me with the following (paraphrased):
public interface Executable<T> {
T execute();
}
The question arose: What if an implementor doesn't have a meaningful value to return?
public class MyExecutable implements Executable<____> {
// ...
}
You could fill in the blank with Object
and return null
or this
, perhaps. However, we agreed that this doesn't communicate as well to its clients the fact that there really isn't a return value and it shouldn't even be used. You can't use primitive void
since it really isn't a type, and is a primitive anyhow (forbidden as a generic type parameter). What to do?
How about java.lang.Void?
public class MyExecutable implements Executable<Void> {
public Void execute() {
// do stuff
return null;
}
}
We felt that this clearly communicated that there's no useful return value for the functor. In fact, since java.lang.Void
is uninstantiable, the only value the functor could return is null
. Nifty, eh? Ever think you'd use java.lang.Void
?
Jaggregate internally uses a number of generic functors that really don't need to return anything. I changed them to return java.lang.Void
instead of Object
, and I'm happy with the results.