-
Notifications
You must be signed in to change notification settings - Fork 611
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[cmd] Refactor RobotModeTriggers to use static variables #7337
[cmd] Refactor RobotModeTriggers to use static variables #7337
Conversation
Signed-off-by: Jade Turner <[email protected]>
This PR modifies commands. Please open a corresponding PR in Python Commands and include a link to this PR. |
/format |
This is a breaking change. What's the motivation for this? |
Signed-off-by: Jade Turner <[email protected]>
Using them in a factory just creates a footgun, it doesn't serve any purpose over them being a field. |
Would definitely prefer to keep them as functions. Fields lock us in way more to having to make breaking changes in the future if something needs to change. As functions, that might not always be required. |
Functions add a really easy way for users to start causing problems with their code. Many teams have had issues with calling Trigger factories in loops or periodically creating huge amounts of objects and binding the check many times. This can cause multitudes of issues, all prevented by this. |
I'd much rather just make the function return a singleton if we wanted that behavior, rather then forcing it into a field. |
Fields are how this should be done in robot code and significantly simpler, why use a singleton? |
Because fields lock you down much more. Plus then this is now a breaking change. Keeping it as a function isn't breaking, and keeps our options in the future. |
Signed-off-by: Jade Turner <[email protected]>
Locking down is the right behaviour here. The only way we'd want to change this is if the underlying function call changed or we needed to do more logic in the check, that can all be done in the constructor to the Triggers |
/format |
Signed-off-by: Jade Turner <[email protected]>
Signed-off-by: Jade Turner <[email protected]>
/format |
I'm with the others here, it should stay a function -- perhaps with a caching field (similar to the HIDs). If already, they should be functions with a defaulted EventLoop parameter (though I accept the claim that the few teams using custom EventLoops can create a trigger manually). (Btw, modifier order should be |
Fields lock us down in the API/ABI sense. Function implementations can change safely without the user noticing; fields are much less flexible in this sense. |
Definitely agree with this change; all logic changes that could potentially rise from creating an autonomous() trigger can be easily put within the lambda of the Trigger class.
In the edge case that this does happen, deprecation is a very easy option. Overall though, running side effects on the creation of an autonomous/teleop/test mode trigger feels like code smell(since the user expects them to be immutable). |
I still don't get the motivation behind this change. Things I've seen in this PR thread (in chronological order):
|
In my eyes removing the extra parentheses is a non-unnoticable boon in code readability, and helps distinguish a method call that returns a value from a Trigger.
Usage reporting will end up over-reporting users that use the trigger multiple times within their code. For instance, these 2 usages wouldn't be equally reported:
So i don't think this should be a usage-reportable statistic in the first place. To accomplish something similar it would be better to add a handler lambda to the Trigger class that allows you to run side effects when a command is registered to the trigger. |
I think that the name of the method combined with the return type is already readable enough. It's not a verb like
You would have to look at the return type anyways to figure out how to use the method.
We already do this kind of usage reporting from static methods.
private static int disabledCallCount = 0;
private static boolean disabledPeriodicWarning; // Bad name, but you know what I mean
public static Trigger disabled() {
if (disabledCallCount < 200 && !disabledPeriodicWarning) {
disabledCallCount++;
} else {
disabledPeriodicWarning = true;
DriverStation.reportWarning("disabled Trigger is being called excessively. This might mean it's being called periodically. This should not be called in a periodic method.", false);
}
return new Trigger(DriverStation::isDisabled);
} |
Makes sense; trigger invocations wouldn't be a problem if we cached them or just made them final properties but i guess if we were to usage-report the autonomous() trigger and such we could. I'm still a believer that it would be better accomplished via an onCommandBind() method of a trigger that can be re-mapped to usage-report the quantity of commands bound to a trigger |
If someone's interested in solving the allocation footgun they can make these singletons |
No description provided.