Skip to content
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

Added withDeadline modifier #7299

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

Daniel1464
Copy link

@Daniel1464 Daniel1464 commented Oct 27, 2024

Currently, a lot of teams use the Commands.parallel(), Commands.race() and Commands.deadline() static factory methods for parallel groups. However, Commands.deadline() doesn't communicate its purpose very well(it doesn't mention the word "parallel" at all, and it doesn't make clear what the deadline of the parallel group is). Even though the deadlineWith(soon to be called deadlineFor) modifier exists, it introduces inconsistency in syntax if a team uses the parallel() and race() factories(as it is an instance method).

Thus, a withDeadline() modifier is added to the ParallelCommandGroup class to allow for Commands.parallel(a,b,c).withDeadline(d). This solves the issues of syntax inconsistency while expressing the intent better to code readers. In addition, the parallel factory methods now have a return type of their respective class, which allows for this syntax to exist.

@Daniel1464 Daniel1464 requested a review from a team as a code owner October 27, 2024 20:55
Copy link
Contributor

This PR modifies commands. Please open a corresponding PR in Python Commands and include a link to this PR.

@spacey-sooty
Copy link
Contributor

Note: I don't believe this is possible in c++ due to the use of CommandPtr.

Pretty sure it is

@Starlight220
Copy link
Member

Note: I don't believe this is possible in c++ due to the use of CommandPtr.

Pretty sure it is

It can't be done in C++ due to the opaqueness of the underlying command type. Specific return types would cause object slicing issues when used (that's why CommandPtr was introduced).


Now onto Java:
I'm not a fan of this.

The return type opaqueness is intentional, to allow for encapsulation and any later implementation changes in a non-breaking way. There's little-to-no advantage to specifying exact command types, and it breaks on any changes.
There used to be type-specific decorators/overrides etc, it caused issues and was removed in favor of consistent polymorphism.

The added method itself doesn't add anything, imo. It doesn't use the fact that it's a parallel group. It's more verbose than the alternatives.

@Daniel1464
Copy link
Author

I doubt that the implementation of Commands.parallel() will change in the future; at most, any overhauls to parallel groups will modify the existing classes and not new ones

@KangarooKoala
Copy link
Contributor

My personal take- I agree that there's significant value in distinguishing the deadline command from the other commands. However, I also agree that it's good library design to keep the return types of the factories as an opaque Command. (This also ensures that we maintain parity with C++)

These two statements lead me to the idea: Add a Command withDeadline(Command deadline) to Command in Java (CommandPtr WithDeadline(CommandPtr&& deadline) && to Command and CommandPtr in C++) which is an alternate way to invoke deadlineFor(). The main downside is that deadlineFor() already exists and is more powerful (since it can accept variadic arguments). I think there's still value is adding an alternate order since depending on the situation it might make more sense to put the non-deadline command first, but I'm ambivalent.

@Starlight220 @Daniel1464 Thoughts?

@Daniel1464
Copy link
Author

Daniel1464 commented Oct 28, 2024

I think that probably would be a better implementation of my current idea.
Something like this?

Commands.parallel(a, b, c).withDeadline(d)

@Daniel1464
Copy link
Author

/format

@Starlight220
Copy link
Member

Re command.withDeadline(); that would be a good idea if there's enough use given the limit of one other command

I guess go for it?

Comment on lines 290 to 296
/**
* Creates a new command that runs this command and the deadline in parallel, finishing when the
* deadline finishes.
*
* @param deadline the deadline of the command group
* @return the decorated command
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should include the standard composition warning. (You can copy-paste from the neighboring methods).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes should be reverted.

Copy link
Contributor

@KangarooKoala KangarooKoala left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, for what it's worth- One of the wpilib devs will also need to review this.

@Daniel1464
Copy link
Author

/format

@@ -3,7 +3,6 @@
# Copyright (c) FIRST and other WPILib contributors.
# Open Source Software; you can modify and/or share it under the terms of
# the WPILib BSD license file in the root directory of this project.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i believe it was something that came up when attempting to merge from main

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to have been a weird interaction between the merge and the formatting. Just manually add the line back to clean up the PR diff.

Copy link
Contributor

@KangarooKoala KangarooKoala left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add a test for this to CommandDecoratorTest.

Comment on lines +312 to +319
/**
* Creates a new command that runs this command
* and the deadline in parallel, finishing when
* the deadline finishes.
*
* @param deadline the deadline of the command group
* @return the decorated command
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Open question: Do we want to include an explicit note that the calling command will be interrupted when the other command finishes? (This would make it match more with DeadlineFor)
Also, do we want to add @see DeadlineFor to the WithDeadline() doc comment and @see WithDeadline to the DeadlineFor doc comment?

(Both of these apply to Java, by the way)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense; i'll get around to it soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants