3

In Scala you can do such things:

trait A[T]

trait B[C[_] <: A[_]] {
    def apply[T](entity: C[T]): T
}

The Java analog would look something like this:

interface A<T>

interface B<C<?> extends A> {
   <T> T apply(entity: C<T>): T
}

You can use B with any type that extends A and use this sub-type's type parameter as an output type of the method.

But it doesn't compile, how would you do it in Java?

8
  • What are you trying to do? Commented Jul 26, 2014 at 12:27
  • Maybe you are looking for Generics? Commented Jul 26, 2014 at 12:29
  • I'm trying to return from the function apply() the value of type T Commented Jul 26, 2014 at 12:29
  • I guess you should explore java.util package. Commented Jul 26, 2014 at 12:30
  • 2
    Perhaps you could edit the question to explain in words what the Scala code does; currently, the question can only be answered by an expert in both languages. Commented Jul 26, 2014 at 12:36

1 Answer 1

6

No, you unfortunately can't do this. Java does not have higher kinded types.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.