Converting from Optional Type to a normal return

Hello,

I’m trying to run a simple method that will return a value if the alliance read by Driverstation is Red or another if it is Blue. I structured it as follows however I get the error “Incompatible operand types Optional<DriverStation.Alliance> and DriverStation.Alliance” which to my understanding is something new in the Drivertstation lib for 2024 as I’ve seen other teams do it this way in 2024. Does anyone know how to convert an Optional to a normal return or structure it differently?

If(Driverstation.getAlliance() == Alliance.Blue){
return blank}
else if (Driverstation.getAlliance() == Alliance.Red){
return blank2}

  • Brennon, Team 599

The Java Optional API provides a number of functions you can use for this. Notably the empty() method will tell you if there’s a valid value, and if the value is valid, get() can be used to retrieve it.

1 Like

try:
Driverstation.getAlliance().get() == Alliance.Blue

just dont forget to check whether its actually available using

empty()

Ripped from the Pathplanner docs:

var alliance = DriverStation.getAlliance();
              if (alliance.isPresent()) {
                return alliance.get() == DriverStation.Alliance.Red;
              }
1 Like