type combinations

T

Thomas Hawtin

Stefan said:
I have a class whose instances are both an A and a B:

interface A {}
interface B {}
class C implements A, B {}

The method »m« needs to declare that it returns somthing that
is also both an A and a B (but not necessarily a C).
I try to express this via the type paramter »T«:

<T extends A & B> T m(){ return new C(); }

It's the caller that determines exactly what T is. T need not be a
subtype of C. What you appear to be trying to do is:

A&B m() { return new C(); } // Illegal.

or

? extends A&B m() { return new C(); } // Still illegal.
I was hoping that the compiler (JDK 1.6 beta) would sse that
»new C()« satisfies this requirement. But the compiler
reports:

C does not always satisfy the requirements. Consider:

class D extends C {}
....
D d = YourClass. said:
How could the code be modified to express that:

- m returns an object that implements (or »extends«) both A and B, and
- »return new C()« is accepted within m's declaration for this purpose?

You cannot use intersection types other than for generic parameters (and
implicitly as the result of the ?: operator).

Tom Hawtin
 
S

Stefan Ram

I have a class whose instances are both an A and a B:

interface A {}
interface B {}
class C implements A, B {}

The method »m« needs to declare that it returns somthing that
is also both an A and a B (but not necessarily a C).
I try to express this via the type paramter »T«:

<T extends A & B> T m(){ return new C(); }

I was hoping that the compiler (JDK 1.6 beta) would sse that
»new C()« satisfies this requirement. But the compiler
reports:

incompatible types
found : C
required: T
{ return new C(); }
1 error ^

How could the code be modified to express that:

- m returns an object that implements (or »extends«) both A and B, and
- »return new C()« is accepted within m's declaration for this purpose?
 
S

s.Siddharth.Sharma

hi Stefan,

You can only return something that is both an A and B by returning an
object of a class which implements these both interfaces given that
both A and B are non related.
The compiler is giving you error because T can be given any data type
same or different than C. Hence, the declared returning data type (T)
might not fall in the same type heirarchy of C.

Regards,
Sidd
 
S

Stefan Ram

Thomas Hawtin said:
What you appear to be trying to do is:
A&B m() { return new C(); } // Illegal.

That is indeed what I'd like to express.
C does not always satisfy the requirements. Consider:
class D extends C {}
D d = YourClass.<D>m();

I see.

Thanks for both the answers to my post!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top