Is there any way to make a generic method work with its type parameter as if it were a number? For example, consider a method that takes any number and returns its value doubled while keeping the input type.
static <T> T foo(T number) {
return number*2;
}
This doesn't compile because *
isn't defined for T
and int
. Changing T
to <T extends Number>
doesn't help, casting (T)2
doesn't compile either.