Hello guys. I was trying to program a new NetworkTable based tool and I found it good to use the Supplier in Java as a parameter in a function. However I bumped into this problem, part of the code should look like the following:
public class PubGroup{
GenericPublisher publisher;
Supplier supplier;
PubGroup(String topicName, Supplier<Boolean> _supp){
supplier = _supp;
publisher = m_table.getTopic(topicName).genericPublish("boolean");
}
}
As you can see, at the third line a “Unknown type” Supplier is defined, however, it becomes a “Boolean” one later, I don’t know if this will create bugs and so. Compiled it and it’s ok.
Any suggestions are welcome!
If you are looking to post data to network tables, I highly recommend Monologue! 6995 made it and it has worked well for us so far. Their docs explain everything. All you need to do is create a global variable, annotate it with @Log, and then update it to whatever you want, whenever you want
Fancy types like Pose2/3d and arrays work too.
Sorry for not responding to the question though. I can’t just give free advertisements with no alternatives! So here’s my take…
I think that’s an okay thing to do with Suppliers. I would give it a type beforehand to make sure that I can remember what the type is throughout the code, and if it is something like a boolean I would personally use a BooleanSupplier. I am getting that this is aimed to be generic, in which case I think this kind of system would work. You could also look at something like using Type T… I would look at the API for WPIMath’s Pair class, they use generic types.
Is there a particular reason you want to pass a Supplier<Boolean> into the constructor but save it as the non-specific Supplier type? If youre planning to have other constructors that accept Supplier<Double> and others, type-erasure will prevent that from working as you might expect (see comments here: Overloading methods with Suppliers as parameters). If you do want to ensure it’s always Boolean, then just make it a BooleanSupplier. If you want it to be generic, then you can make your class generic with <T> on the class, and Supplier<T> for the member and constructor.