Re: Generics - Is this possible?
lstephen wrote:
Hi,
I'm looking at the signature for something like a 'map' function.
'map' is a somewhat unfortunate variable name, since you aren't actually
associating it with the wildly popular 'Map' type.
For List it may be something like:
List<B> map(List<A> a, UnaryFunction<A, B> f)
But, I want I'd rather it not be List specific, so I was after
something like:
T<B> map(T<A> a UnaryFunction<A, B> f)
But, the compiler doesn't like this ;)
Any ideas on how or whether this is possible?
What you want to accomplish is possible, what you're trying to say isn't.
You're trying to indicate a type parameter that takes a type parameter; that
doesn't exist. Type parameters exist to restrict the range of acceptable
types; what you wrote, 'T<A>', is equivalent to merely 'T'.
What is the restriction that you actually want to place on T? Do you want it
to be a Collection?
public static <A, B>
Collection <B> xform( Collection <A> a, UnaryFunction <A, B> f )
(untried, untested, uncompiled, one of several solutions that differ subtly in
semantics)
If you have a class called 'UnaryFunction', shouldn't it overload its "unary
function" method with one that takes a Collection? Having a "friend" method
like that shown breaks encapsulation.
--
Lew