-
Notifications
You must be signed in to change notification settings - Fork 19
Description
I've been using a new pattern for writing Commands that I really like, and I think other people might like it too.
There are two annoyances that come with using the existing helper Command classes.
One annoyance is that the lambdas introduce a lot of syntactic noise that the IDE doesn't handle well. Every time I use the lambda based helper classes, I get a pile of red underlines until I get the right number of matching parentheses and braces. Visually, it is hard to distinguish which command stage each lambda represents.
The other annoyance is that the helper classes don't have an easy and obvious way to add new member variables when used as originally intended.
Introducing a potential new library helper class...
public class OverrideCommand extends FunctionalCommand {
public OverrideCommand(Subsystem... requirements) {
super (()->{},
()->{},
(boolean interrupted)->{},
()->false, requirements);
}
}
This helper class only accepts subsystem requirements in its constructor. You use it by combining it with Java's anonymous class feature:
public Command cmdTuneKs() {
return new OverrideCommand (this) {
private double myNewVariable;
@Override
public void initialize() {
myNewVariable = 0d;
}
};
}
Notice that there are no lambdas in the subsystem code and we can override any of the methods of the Command. (Also note that unless overridden, the command is not instant by default.) We also don't have to supply empty lambdas for the methods we don't want to override. The resulting user code is substantially clearer because the names of the overridden methods appear directly in the code. The method returns a Command, eliminating the need for the library user to write a separate class and class factory.
Of course, you don't strictly need an OverrideCommand. You could do the same trick with FunctionalCommand, but it would be syntactically noisier.
Anyway, I thought it might be worthwhile to add this class to the library and the pattern to the documentation.