tags:

views:

768

answers:

1

Hi,

I've got some generic class for my JPA model POJO that goes like this:

public interface Identifiable<PK extends Serializable> {
    PK getUniqueId();
}

public interface GenericDao<T extends Identifiable<PK>> {
    public T findById(PK id);
}

This code won't compile. For this to work, I need to specify

public interface GenericDao<T extends Identifiable<PK>, PK extends Serializable>

But that's redundant information !! The fact that T extends Identifiable imply that a PK type will be specified for the Identifiable instance and that this is the type to use for the DAO's PK.

How can I make this work without redundant information ?

Thanks, Fred


Edit: Simplified example

+2  A: 

Have you tried:

public interface WatchableDao<T extends Watchable<?>>

(i.e. it's a Watchable<Something> but I don't care what Something is)

I haven't tried it, but it's worth a go.

EDIT: Post question edit, it seems that you really do need PK as a type parameter to the interface. In that case, I believe you have to effectively repeat the constraint as you are doing. Yes, it's redundant, but I think it's simpler than the language having to specify what effective constraints would apply to PK based on its use as a type argument elsewhere. If it's any consolation, the same is true in C#.

It also makes the constraints on PK clear from just the interface itself, rather than having to look at another interface to see what's feasible.

Jon Skeet
It doesn't work because GenericDao<T, PK> was needing PK to be specified... I simplified my example. You should understand it better now.
Frederic Morin
Editing appropriately :)
Jon Skeet
It won't compile since function public T findById(PK id) need the PK generic type to be defined. Maybe I'll just live with redundant info...
Frederic Morin
Thanks for your reply. Event if it's a negative answer I'll thread that as accepted :)
Frederic Morin