Convert Array of DoubleSupplier to Supplier<Double>

I’ve recently begun to implement BadLog into our robot code. I have an array with elements of type DoubleSupplier that I want to log, which I wished to use a loop for, however, BadLog can only use the type Supplier<Double> in the method that attaches a supplier to log. Is there any way to convert an array with elements of type DoubleSupplier to individual suppliers of type Supplier<Double>?

Supplier<Double> supplierOfDoubleAtIndex0 = () -> doubleSupplierArray[0].getAsDouble();

This works fine as long as you have a specific index you want to use, but if you try to loop through this it will not work because the index variable is out of scope

The index variable is captured by the lambda; it works fine. If the Java compiler complains, it’s probably due to it not being able to verify that the variable is effectively final within each iteration of the loop; you can fix this by simply using a dummy variable assigned to the value of the index:

for (int i = 0; i<n; i++) {
  final int ii = i;
  // use ii as your captured variable in the rest of the loop
}
2 Likes