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

Add Commands.runIf(...) as command factory method to Commands #7808

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

Conversation

otter502
Copy link

@otter502 otter502 commented Feb 20, 2025

Intends to close #7807

Adds the equivalent to an if statement for Commands.java.
It runs Commands.either under the hood with Commands.none() as the onFalse parameter.

I haven't coded a C++ or python version for this as I'm not confident enough in my coding ability in those languages.

Copy link
Contributor

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

@github-actions github-actions bot added the component: command-based WPILib Command Based Library label Feb 20, 2025
@otter502 otter502 marked this pull request as ready for review February 20, 2025 06:33
@otter502 otter502 requested a review from a team as a code owner February 20, 2025 06:33
/**
* Runs a command if the boolean selector function is true.
*
* @param selector the selector function
Copy link
Member

Choose a reason for hiding this comment

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

Instead of selector I'd refer to this as a condition

Copy link
Author

@otter502 otter502 Feb 20, 2025

Choose a reason for hiding this comment

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

@rzblue With this change should I move the command out of the Selector Commands section of the Commands.java file? If so where should it go instead?

Copy link
Member

Choose a reason for hiding this comment

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

It can stay in the Selecting section (along with either etc)

@otter502 otter502 requested a review from rzblue February 20, 2025 07:26
@@ -166,6 +166,18 @@ public static Command either(Command onTrue, Command onFalse, BooleanSupplier se
return new ConditionalCommand(onTrue, onFalse, selector);
}

/**
* Runs a command if the boolean condition function is true.
Copy link
Member

Choose a reason for hiding this comment

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

Make it more obvious in the doc that if the condition is false, the command will do nothing and exit (meaning it will still grab requirements etc) -- you can link to none()

Copy link
Author

Choose a reason for hiding this comment

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

I edited the code so it should be clear now!
Apologies for the late response!

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.

Nice work so far!

@@ -166,6 +166,19 @@ public static Command either(Command onTrue, Command onFalse, BooleanSupplier se
return new ConditionalCommand(onTrue, onFalse, selector);
}

/**
* Runs a command if the boolean condition function is true otherwise it does nothing and exits.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* Runs a command if the boolean condition function is true otherwise it does nothing and exits.
* Runs a command if the boolean condition function is true. Otherwise it does nothing and exits
* (but still has the same requirements as the command, interrupting any commands with shared
* requirements).

I could see some people reading "does nothing" as "has no side effects" and being surprised/confused when it interrupts commands with shared requirements. Splitting the sentence is just personal preference that helps me parse it better, but go ahead and structure it how you think is best.

* @param onTrue the command to run if the condition function returns true
* @return the command
* @see ConditionalCommand
* @see {@link Commands#none()}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* @see {@link Commands#none()}
* @see Commands#none()

When running the java formatting CI locally, it didn't seem to like using @link in the @see. This has the following javadoc output (you can run ./gradlew wpilibNewCommands:javadoc locally and open wpilibNewCommands/build/docs/javadoc/allclasses-index.html to see the javadoc output):
image
(This is including the other suggested change)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: command-based WPILib Command Based Library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Commands.runIf
4 participants