Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top