I have this interface:
public interface Command {
T execute(String... args);
}
it works fine for most uses. But when I try to mode
Keep the interface as it is: this is why Void is in the Standard Library. Just as long as whatever is calling the Command expects nulls to come back out.
And yes, null is the only value you can return for Void.
For the last several years I have avoided Void
as return types except where reflection is concerned. I have used a different pattern which I think is more explicit and avoids nulls. That is, I have a success type, which I call Ok
which is returned for all commands like the OP's. This has worked very well for my team and has also propagated into other teams' use.
public enum Ok { OK; }
public class SideEffectCommand implements Command {
@Override
public Ok execute(String... args) {
...
return Ok.OK; // I typically static import Ok.OK;
}