Reputation: 3596
I have the following function which returns an List
of the same type as which it is passed.
<T> List<T> foo(T bar)
{
...
}
I want to have this same function, except in the form of a Java Functional Interface. I've tried the following:
final <T> Function<T, List<T>> foo;
But it does not like the <T>
. If I omit <T>
like:
final Function<T, List<T>> foo;
It claims it cannot find type T
, exactly the same error as if I were to define the original function as:
List<T> foo(T bar) // cannot find type 'T'
{
...
}
I want to have function foo
as a first class function.
Upvotes: 0
Views: 70
Reputation: 425003
You’re confusing a method with a class, which may be implemented as a lambda.
If T
isn’t a type in the context of the code, you can’t type anything.
You can do this:
class MyClass<T> implements Function<T, List<T>> {
public List<T> accept(T t) {
// some impl
}
}
Upvotes: 3