Reputation: 8346
I am trying to code up a few Function
s that I will be using quite often in the rest of my code. Some of these are things like, say, summing up all the numbers of an Iterable
. Instead of implementing different functions for Double
, Integer
, etc. I want a single Function that sums up all the elements of a given Iterable as long as each element is a number. In order to do this, I attempted the following:
<T> Function<List<T extends Number>, T> sum = new Function<List<T extends Number>, T>() {
public T apply(List<T> tlist) {
// the main code
}
};
But I get a compiler error saying "Unexpected token". I have coded my own Java methods with generics before, but I don't understand why the same thing isn't working here. Am I missing something ridiculously obvious, or is it that guava Functions don't allow generics?
Upvotes: 1
Views: 950
Reputation: 110046
A couple things:
T extends Number
) you have to do it where the type is declared (e.g. not <T> Function...
but <T extends Number> Function...
). I see Louis has already covered this.Number
, though you could come reasonably close I suppose. You'd have to take into consideration the possibility of numbers that are larger than any of the built in primitive types can represent (BigInteger
, BigDecimal
, and even possibly custom Number
subclasses).Double
.It can just be:
Function<List<? extends Number>, Double> sum = new Function<List<? extends Number, Double>() {
public Double apply(List<? extends Number> numbers) {
// the main code
}
}
Upvotes: 2
Reputation: 198033
You can't define a polymorphic variable like this, and you can also only put bounds like extends Number
inside the initial declaration of the T
type variable. I suspect that the closest thing to what you mean that would actually work is something like
<T extends Number> Function<List<T>, T> sum() {
return new Function<List<T>, T>() {
public Double apply(List<T> doubles) {
// the main code
}
};
}
...except that you're also going to have issues returning a Double
instead of a T
.
Upvotes: 1