views:

94

answers:

1

Does Googe Guava for Java have a Function inner class with a void return value, like C#'s action? I'm tired of making a bunch of Function<Float, Integer> with meaningless return values.

+5  A: 

You could use a Function<Float, Void> and have it return null to make your intentions more clear. Or you could make your own abstract class that implements Function<T, Void> too if you want.

But no, Guava doesn't have anything like that... it also doesn't have any methods designed to use something like that, such as a forEach type method. If you're using Guava's methods that use Functions with a Function<Float, Void> or some such... I wouldn't really recommend that, since the methods aren't designed for that (even if you can with some of them). If you're using your own methods for this, I'd say just make your own interface like Callback<T>.

ColinD
Isn't it something like a closure ?
Sylvain M
@Sylvain: Any anonymous inner class declared in a method is a closure that closes over fields in the enclosing class and final variables in the method it's declared in. Non-static inner classes declared elsewhere also close over the fields of their containing class.
ColinD
Ok, thx for the explanation, so if I understand well, this case is just an example of closure. I'm right ?
Sylvain M
@Sylvain: Right. Closure is a pretty broad term. A lot of people also use "closure" when they mean "lambda expression".
ColinD