[commands] Deprecate Command.schedule() (#7072)

It's only syntactic sugar over the CommandScheduler's schedule method and creates a footgun because it’s too obvious to try to use in incorrect places.

Co-authored-by: Starlight220 <53231611+Starlight220@users.noreply.github.com>
This commit is contained in:
Gold856
2025-07-02 16:46:59 -04:00
committed by GitHub
parent a3ce880334
commit 26771e38fb
42 changed files with 77 additions and 66 deletions

View File

@@ -533,7 +533,12 @@ public abstract class Command implements Sendable {
});
}
/** Schedules this command. */
/**
* Schedules this command.
*
* @deprecated Use CommandScheduler.getInstance().schedule(Command...) instead
*/
@Deprecated(since = "2025", forRemoval = true)
public void schedule() {
CommandScheduler.getInstance().schedule(this);
}
@@ -610,7 +615,7 @@ public abstract class Command implements Sendable {
value -> {
if (value) {
if (!isScheduled()) {
schedule();
CommandScheduler.getInstance().schedule(this);
}
} else {
if (isScheduled()) {

View File

@@ -58,7 +58,7 @@ public class ProxyCommand extends Command {
@Override
public void initialize() {
m_command = m_supplier.get();
m_command.schedule();
CommandScheduler.getInstance().schedule(m_command);
}
@Override

View File

@@ -28,7 +28,7 @@ public class ScheduleCommand extends Command {
@Override
public void initialize() {
for (Command command : m_toSchedule) {
command.schedule();
CommandScheduler.getInstance().schedule(command);
}
}

View File

@@ -93,7 +93,7 @@ public class Trigger implements BooleanSupplier {
addBinding(
(previous, current) -> {
if (previous != current) {
command.schedule();
CommandScheduler.getInstance().schedule(command);
}
});
return this;
@@ -110,7 +110,7 @@ public class Trigger implements BooleanSupplier {
addBinding(
(previous, current) -> {
if (!previous && current) {
command.schedule();
CommandScheduler.getInstance().schedule(command);
}
});
return this;
@@ -127,7 +127,7 @@ public class Trigger implements BooleanSupplier {
addBinding(
(previous, current) -> {
if (previous && !current) {
command.schedule();
CommandScheduler.getInstance().schedule(command);
}
});
return this;
@@ -148,7 +148,7 @@ public class Trigger implements BooleanSupplier {
addBinding(
(previous, current) -> {
if (!previous && current) {
command.schedule();
CommandScheduler.getInstance().schedule(command);
} else if (previous && !current) {
command.cancel();
}
@@ -171,7 +171,7 @@ public class Trigger implements BooleanSupplier {
addBinding(
(previous, current) -> {
if (previous && !current) {
command.schedule();
CommandScheduler.getInstance().schedule(command);
} else if (!previous && current) {
command.cancel();
}
@@ -193,7 +193,7 @@ public class Trigger implements BooleanSupplier {
if (command.isScheduled()) {
command.cancel();
} else {
command.schedule();
CommandScheduler.getInstance().schedule(command);
}
}
});
@@ -214,7 +214,7 @@ public class Trigger implements BooleanSupplier {
if (command.isScheduled()) {
command.cancel();
} else {
command.schedule();
CommandScheduler.getInstance().schedule(command);
}
}
});

View File

@@ -197,7 +197,7 @@ void Command::InitSendable(wpi::SendableBuilder& builder) {
[this](bool value) {
bool isScheduled = IsScheduled();
if (value && !isScheduled) {
Schedule();
CommandScheduler::GetInstance().Schedule(this);
} else if (!value && isScheduled) {
Cancel();
}

View File

@@ -37,7 +37,7 @@ ProxyCommand::ProxyCommand(std::unique_ptr<Command> command) {
void ProxyCommand::Initialize() {
m_command = m_supplier();
m_command->Schedule();
frc2::CommandScheduler::GetInstance().Schedule(m_command);
}
void ProxyCommand::End(bool interrupted) {

View File

@@ -18,7 +18,7 @@ ScheduleCommand::ScheduleCommand(Command* toSchedule) {
void ScheduleCommand::Initialize() {
for (auto command : m_toSchedule) {
command->Schedule();
frc2::CommandScheduler::GetInstance().Schedule(command);
}
}

View File

@@ -29,7 +29,7 @@ void Trigger::AddBinding(wpi::unique_function<void(bool, bool)>&& body) {
Trigger Trigger::OnChange(Command* command) {
AddBinding([command](bool previous, bool current) {
if (previous != current) {
command->Schedule();
frc2::CommandScheduler::GetInstance().Schedule(command);
}
});
return *this;
@@ -38,7 +38,7 @@ Trigger Trigger::OnChange(Command* command) {
Trigger Trigger::OnChange(CommandPtr&& command) {
AddBinding([command = std::move(command)](bool previous, bool current) {
if (previous != current) {
command.Schedule();
frc2::CommandScheduler::GetInstance().Schedule(command);
}
});
return *this;
@@ -47,7 +47,7 @@ Trigger Trigger::OnChange(CommandPtr&& command) {
Trigger Trigger::OnTrue(Command* command) {
AddBinding([command](bool previous, bool current) {
if (!previous && current) {
command->Schedule();
frc2::CommandScheduler::GetInstance().Schedule(command);
}
});
return *this;
@@ -56,7 +56,7 @@ Trigger Trigger::OnTrue(Command* command) {
Trigger Trigger::OnTrue(CommandPtr&& command) {
AddBinding([command = std::move(command)](bool previous, bool current) {
if (!previous && current) {
command.Schedule();
frc2::CommandScheduler::GetInstance().Schedule(command);
}
});
return *this;
@@ -65,7 +65,7 @@ Trigger Trigger::OnTrue(CommandPtr&& command) {
Trigger Trigger::OnFalse(Command* command) {
AddBinding([command](bool previous, bool current) {
if (previous && !current) {
command->Schedule();
frc2::CommandScheduler::GetInstance().Schedule(command);
}
});
return *this;
@@ -74,7 +74,7 @@ Trigger Trigger::OnFalse(Command* command) {
Trigger Trigger::OnFalse(CommandPtr&& command) {
AddBinding([command = std::move(command)](bool previous, bool current) {
if (previous && !current) {
command.Schedule();
frc2::CommandScheduler::GetInstance().Schedule(command);
}
});
return *this;
@@ -83,7 +83,7 @@ Trigger Trigger::OnFalse(CommandPtr&& command) {
Trigger Trigger::WhileTrue(Command* command) {
AddBinding([command](bool previous, bool current) {
if (!previous && current) {
command->Schedule();
frc2::CommandScheduler::GetInstance().Schedule(command);
} else if (previous && !current) {
command->Cancel();
}
@@ -94,7 +94,7 @@ Trigger Trigger::WhileTrue(Command* command) {
Trigger Trigger::WhileTrue(CommandPtr&& command) {
AddBinding([command = std::move(command)](bool previous, bool current) {
if (!previous && current) {
command.Schedule();
frc2::CommandScheduler::GetInstance().Schedule(command);
} else if (previous && !current) {
command.Cancel();
}
@@ -105,7 +105,7 @@ Trigger Trigger::WhileTrue(CommandPtr&& command) {
Trigger Trigger::WhileFalse(Command* command) {
AddBinding([command](bool previous, bool current) {
if (previous && !current) {
command->Schedule();
frc2::CommandScheduler::GetInstance().Schedule(command);
} else if (!previous && current) {
command->Cancel();
}
@@ -116,7 +116,7 @@ Trigger Trigger::WhileFalse(Command* command) {
Trigger Trigger::WhileFalse(CommandPtr&& command) {
AddBinding([command = std::move(command)](bool previous, bool current) {
if (!previous && current) {
command.Schedule();
frc2::CommandScheduler::GetInstance().Schedule(command);
} else if (previous && !current) {
command.Cancel();
}
@@ -130,7 +130,7 @@ Trigger Trigger::ToggleOnTrue(Command* command) {
if (command->IsScheduled()) {
command->Cancel();
} else {
command->Schedule();
frc2::CommandScheduler::GetInstance().Schedule(command);
}
}
});
@@ -143,7 +143,7 @@ Trigger Trigger::ToggleOnTrue(CommandPtr&& command) {
if (command.IsScheduled()) {
command.Cancel();
} else {
command.Schedule();
frc2::CommandScheduler::GetInstance().Schedule(command);
}
}
});
@@ -156,7 +156,7 @@ Trigger Trigger::ToggleOnFalse(Command* command) {
if (command->IsScheduled()) {
command->Cancel();
} else {
command->Schedule();
frc2::CommandScheduler::GetInstance().Schedule(command);
}
}
});
@@ -169,7 +169,7 @@ Trigger Trigger::ToggleOnFalse(CommandPtr&& command) {
if (command.IsScheduled()) {
command.Cancel();
} else {
command.Schedule();
frc2::CommandScheduler::GetInstance().Schedule(command);
}
}
});

View File

@@ -398,7 +398,10 @@ class Command : public wpi::Sendable, public wpi::SendableHelper<Command> {
/**
* Schedules this command.
*
* @deprecated Use CommandScheduler::GetInstance().Schedule() instead
*/
[[deprecated("Use CommandScheduler::GetInstance().Schedule() instead.")]]
void Schedule();
/**

View File

@@ -277,7 +277,10 @@ CommandPtr final {
/**
* Schedules this command.
*
* @deprecated Use CommandScheduler::GetInstance().Schedule() instead
*/
[[deprecated("Use CommandScheduler::GetInstance().Schedule() instead.")]]
void Schedule() const&;
// Prevent calls on a temporary, as the returned pointer would be invalid

View File

@@ -55,7 +55,7 @@ class CommandSendableButtonTest extends CommandTestBase {
@Test
void trueAndScheduledNoOp() {
// Scheduled and true -> no-op
m_command.schedule();
CommandScheduler.getInstance().schedule(m_command);
CommandScheduler.getInstance().run();
SmartDashboard.updateValues();
assertTrue(m_command.isScheduled());
@@ -90,7 +90,7 @@ class CommandSendableButtonTest extends CommandTestBase {
@Test
void falseAndScheduledCancel() {
// Scheduled and false -> cancel
m_command.schedule();
CommandScheduler.getInstance().schedule(m_command);
CommandScheduler.getInstance().run();
SmartDashboard.updateValues();
assertTrue(m_command.isScheduled());

View File

@@ -22,7 +22,7 @@ class ProxyCommandTest extends CommandTestBase {
scheduler.schedule(scheduleCommand);
verify(command1).schedule();
verify(command1).initialize();
}
}

View File

@@ -22,8 +22,8 @@ class ScheduleCommandTest extends CommandTestBase {
scheduler.schedule(scheduleCommand);
verify(command1).schedule();
verify(command2).schedule();
verify(command1).initialize();
verify(command2).initialize();
}
}

View File

@@ -39,10 +39,10 @@ class NetworkButtonTest extends CommandTestBase {
pub.set(false);
button.onTrue(command);
scheduler.run();
verify(command, never()).schedule();
verify(command, never()).initialize();
pub.set(true);
scheduler.run();
scheduler.run();
verify(command).schedule();
verify(command).initialize();
}
}

View File

@@ -207,7 +207,7 @@ class TriggerTest extends CommandTestBase {
.until(button);
button.setPressed(false);
command1.schedule();
scheduler.schedule(command1);
scheduler.run();
assertEquals(1, startCounter.get());
assertEquals(0, endCounter.get());
@@ -258,13 +258,13 @@ class TriggerTest extends CommandTestBase {
button.setPressed(true);
scheduler.run();
verify(command, never()).schedule();
verify(command, never()).initialize();
SimHooks.stepTiming(0.3);
button.setPressed(true);
scheduler.run();
verify(command).schedule();
verify(command).initialize();
}
@Test

View File

@@ -49,7 +49,7 @@ TEST_F(CommandSendableButtonTest, trueAndNotScheduledSchedules) {
TEST_F(CommandSendableButtonTest, trueAndScheduledNoOp) {
// Scheduled and true -> no-op
m_command->Schedule();
frc2::CommandScheduler::GetInstance().Schedule(m_command.value());
GetScheduler().Run();
frc::SmartDashboard::UpdateValues();
EXPECT_TRUE(m_command->IsScheduled());
@@ -82,7 +82,7 @@ TEST_F(CommandSendableButtonTest, falseAndNotScheduledNoOp) {
TEST_F(CommandSendableButtonTest, falseAndScheduledCancel) {
// Scheduled and false -> cancel
m_command->Schedule();
frc2::CommandScheduler::GetInstance().Schedule(m_command.value());
GetScheduler().Run();
frc::SmartDashboard::UpdateValues();
EXPECT_TRUE(m_command->IsScheduled());

View File

@@ -38,7 +38,7 @@ void Robot::AutonomousInit() {
m_autonomousCommand = m_container.GetAutonomousCommand();
if (m_autonomousCommand) {
m_autonomousCommand->Schedule();
frc2::CommandScheduler::GetInstance().Schedule(m_autonomousCommand.value());
}
}

View File

@@ -47,7 +47,7 @@ void Robot::AutonomousInit() {
m_autonomousCommand = m_container.GetAutonomousCommand();
if (m_autonomousCommand != nullptr) {
m_autonomousCommand->Schedule();
frc2::CommandScheduler::GetInstance().Schedule(m_autonomousCommand);
}
}

View File

@@ -47,7 +47,7 @@ void Robot::AutonomousInit() {
m_autonomousCommand = m_container.GetAutonomousCommand();
if (m_autonomousCommand != nullptr) {
m_autonomousCommand->Schedule();
frc2::CommandScheduler::GetInstance().Schedule(m_autonomousCommand);
}
}

View File

@@ -38,7 +38,7 @@ void Robot::AutonomousInit() {
m_autonomousCommand = m_container.GetAutonomousCommand();
if (m_autonomousCommand) {
m_autonomousCommand->Schedule();
frc2::CommandScheduler::GetInstance().Schedule(m_autonomousCommand.value());
}
}

View File

@@ -26,7 +26,7 @@ void Robot::AutonomousInit() {
m_autonomousCommand = m_robot.GetAutonomousCommand();
if (m_autonomousCommand) {
m_autonomousCommand->Schedule();
frc2::CommandScheduler::GetInstance().Schedule(m_autonomousCommand.value());
}
}

View File

@@ -37,7 +37,7 @@ void Robot::AutonomousInit() {
m_autonomousCommand = m_container.GetAutonomousCommand();
if (m_autonomousCommand != nullptr) {
m_autonomousCommand->Schedule();
frc2::CommandScheduler::GetInstance().Schedule(m_autonomousCommand);
}
}

View File

@@ -38,7 +38,7 @@ void Robot::AutonomousInit() {
m_autonomousCommand = m_container.GetAutonomousCommand();
if (m_autonomousCommand != nullptr) {
m_autonomousCommand->Schedule();
frc2::CommandScheduler::GetInstance().Schedule(m_autonomousCommand);
}
}

View File

@@ -38,7 +38,7 @@ void Robot::AutonomousInit() {
m_autonomousCommand = m_container.GetAutonomousCommand();
if (m_autonomousCommand) {
m_autonomousCommand->Schedule();
frc2::CommandScheduler::GetInstance().Schedule(m_autonomousCommand.value());
}
}

View File

@@ -22,7 +22,7 @@ void Robot::AutonomousInit() {
m_autonomousCommand = m_container.GetAutonomousCommand();
if (m_autonomousCommand) {
m_autonomousCommand->Schedule();
frc2::CommandScheduler::GetInstance().Schedule(m_autonomousCommand.value());
}
}

View File

@@ -37,7 +37,7 @@ void Robot::AutonomousInit() {
m_autonomousCommand = m_container.GetAutonomousCommand();
if (m_autonomousCommand != nullptr) {
m_autonomousCommand->Schedule();
frc2::CommandScheduler::GetInstance().Schedule(m_autonomousCommand);
}
}

View File

@@ -37,7 +37,7 @@ void Robot::AutonomousInit() {
m_autonomousCommand = m_container.GetAutonomousCommand();
if (m_autonomousCommand) {
m_autonomousCommand->Schedule();
frc2::CommandScheduler::GetInstance().Schedule(m_autonomousCommand.value());
}
}

View File

@@ -22,7 +22,7 @@ void Robot::AutonomousInit() {
m_autonomousCommand = m_container.GetAutonomousCommand();
if (m_autonomousCommand) {
m_autonomousCommand->Schedule();
frc2::CommandScheduler::GetInstance().Schedule(m_autonomousCommand.value());
}
}

View File

@@ -65,7 +65,7 @@ public class Robot extends TimedRobot {
// schedule the autonomous command (example)
if (m_autonomousCommand != null) {
m_autonomousCommand.schedule();
CommandScheduler.getInstance().schedule(m_autonomousCommand);
}
}

View File

@@ -67,7 +67,7 @@ public class Robot extends TimedRobot {
// schedule the autonomous command (example)
if (m_autonomousCommand != null) {
m_autonomousCommand.schedule();
CommandScheduler.getInstance().schedule(m_autonomousCommand);
}
}

View File

@@ -74,7 +74,7 @@ public class Robot extends TimedRobot {
// schedule the autonomous command (example)
if (m_autonomousCommand != null) {
m_autonomousCommand.schedule();
CommandScheduler.getInstance().schedule(m_autonomousCommand);
}
}

View File

@@ -65,7 +65,7 @@ public class Robot extends TimedRobot {
// schedule the autonomous command (example)
if (m_autonomousCommand != null) {
m_autonomousCommand.schedule();
CommandScheduler.getInstance().schedule(m_autonomousCommand);
}
}

View File

@@ -63,7 +63,7 @@ public class Robot extends TimedRobot {
m_autonomousCommand = m_robot.getAutonomousCommand();
if (m_autonomousCommand != null) {
m_autonomousCommand.schedule();
CommandScheduler.getInstance().schedule(m_autonomousCommand);
}
}

View File

@@ -58,7 +58,7 @@ public class Robot extends TimedRobot {
// schedule the autonomous command (example)
if (m_autonomousCommand != null) {
m_autonomousCommand.schedule();
CommandScheduler.getInstance().schedule(m_autonomousCommand);
}
}

View File

@@ -65,7 +65,7 @@ public class Robot extends TimedRobot {
// schedule the autonomous command (example)
if (m_autonomousCommand != null) {
m_autonomousCommand.schedule();
CommandScheduler.getInstance().schedule(m_autonomousCommand);
}
}

View File

@@ -65,7 +65,7 @@ public class Robot extends TimedRobot {
// schedule the autonomous command (example)
if (m_autonomousCommand != null) {
m_autonomousCommand.schedule();
CommandScheduler.getInstance().schedule(m_autonomousCommand);
}
}

View File

@@ -55,7 +55,7 @@ public class Robot extends TimedRobot {
m_autonomousCommand = m_robot.getAutonomousCommand();
if (m_autonomousCommand != null) {
m_autonomousCommand.schedule();
CommandScheduler.getInstance().schedule(m_autonomousCommand);
}
}

View File

@@ -58,7 +58,7 @@ public class Robot extends TimedRobot {
// schedule the autonomous command (example)
if (m_autonomousCommand != null) {
m_autonomousCommand.schedule();
CommandScheduler.getInstance().schedule(m_autonomousCommand);
}
}

View File

@@ -58,7 +58,7 @@ public class Robot extends TimedRobot {
// schedule the autonomous command (example)
if (m_autonomousCommand != null) {
m_autonomousCommand.schedule();
CommandScheduler.getInstance().schedule(m_autonomousCommand);
}
}

View File

@@ -36,7 +36,7 @@ public class Robot extends TimedRobot {
m_autonomousCommand = m_robotContainer.getAutonomousCommand();
if (m_autonomousCommand != null) {
m_autonomousCommand.schedule();
CommandScheduler.getInstance().schedule(m_autonomousCommand);
}
}

View File

@@ -58,7 +58,7 @@ public class Robot extends TimedRobot {
// schedule the autonomous command (example)
if (m_autonomousCommand != null) {
m_autonomousCommand.schedule();
CommandScheduler.getInstance().schedule(m_autonomousCommand);
}
}

View File

@@ -58,7 +58,7 @@ public class Robot extends TimedRobot {
// schedule the autonomous command (example)
if (m_autonomousCommand != null) {
m_autonomousCommand.schedule();
CommandScheduler.getInstance().schedule(m_autonomousCommand);
}
}